| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "strings" |
| 9 "sync" | 10 "sync" |
| 10 | 11 |
| 11 ds "github.com/luci/gae/service/datastore" | 12 ds "github.com/luci/gae/service/datastore" |
| 12 "github.com/luci/luci-go/common/logging/memlogger" | 13 "github.com/luci/luci-go/common/logging/memlogger" |
| 13 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
| 14 ) | 15 ) |
| 15 | 16 |
| 16 var serializationDeterministic = false | 17 var serializationDeterministic = false |
| 17 | 18 |
| 18 type memContextObj interface { | 19 type memContextObj interface { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 return true | 83 return true |
| 83 } | 84 } |
| 84 | 85 |
| 85 func (m *memContext) applyTxn(c context.Context, txnCtxObj memContextObj) { | 86 func (m *memContext) applyTxn(c context.Context, txnCtxObj memContextObj) { |
| 86 txnCtx := *txnCtxObj.(*memContext) | 87 txnCtx := *txnCtxObj.(*memContext) |
| 87 for i := range *m { | 88 for i := range *m { |
| 88 (*m)[i].applyTxn(c, txnCtx[i]) | 89 (*m)[i].applyTxn(c, txnCtx[i]) |
| 89 } | 90 } |
| 90 } | 91 } |
| 91 | 92 |
| 92 // Use calls UseWithAppID with the appid of "dev~app" | 93 // Use calls UseWithAppID with the appid of "app" |
| 93 func Use(c context.Context) context.Context { | 94 func Use(c context.Context) context.Context { |
| 94 return UseWithAppID(c, "dev~app") | 95 return UseWithAppID(c, "dev~app") |
| 95 } | 96 } |
| 96 | 97 |
| 97 // UseWithAppID adds implementations for the following gae services to the | 98 // UseWithAppID adds implementations for the following gae services to the |
| 98 // context: | 99 // context: |
| 99 // * github.com/luci/gae/service/datastore | 100 // * github.com/luci/gae/service/datastore |
| 100 // * github.com/luci/gae/service/info | 101 // * github.com/luci/gae/service/info |
| 101 // * github.com/luci/gae/service/mail | 102 // * github.com/luci/gae/service/mail |
| 102 // * github.com/luci/gae/service/memcache | 103 // * github.com/luci/gae/service/memcache |
| 103 // * github.com/luci/gae/service/taskqueue | 104 // * github.com/luci/gae/service/taskqueue |
| 104 // * github.com/luci/gae/service/user | 105 // * github.com/luci/gae/service/user |
| 105 // * github.com/luci/luci-go/common/logger (using memlogger) | 106 // * github.com/luci/luci-go/common/logger (using memlogger) |
| 106 // | 107 // |
| 107 // The application id wil be set to 'aid', and will not be modifiable in this | 108 // The application id wil be set to 'aid', and will not be modifiable in this |
| 108 // context. | 109 // context. If 'aid' contains a "~" character, it will be treated as the |
| 110 // fully-qualified App ID and the AppID will be the string following the "~". |
| 109 // | 111 // |
| 110 // These can be retrieved with the gae.Get functions. | 112 // These can be retrieved with the gae.Get functions. |
| 111 // | 113 // |
| 112 // The implementations are all backed by an in-memory implementation, and start | 114 // The implementations are all backed by an in-memory implementation, and start |
| 113 // with an empty state. | 115 // with an empty state. |
| 114 // | 116 // |
| 115 // Using this more than once per context.Context will cause a panic. | 117 // Using this more than once per context.Context will cause a panic. |
| 116 func UseWithAppID(c context.Context, aid string) context.Context { | 118 func UseWithAppID(c context.Context, aid string) context.Context { |
| 117 if c.Value(memContextKey) != nil { | 119 if c.Value(memContextKey) != nil { |
| 118 panic(errors.New("memory.Use: called twice on the same Context")
) | 120 panic(errors.New("memory.Use: called twice on the same Context")
) |
| 119 } | 121 } |
| 120 c = memlogger.Use(c) | 122 c = memlogger.Use(c) |
| 121 | 123 |
| 122 » memctx := newMemContext(aid) | 124 » fqAppID := aid |
| 125 » if parts := strings.SplitN(fqAppID, "~", 2); len(parts) == 2 { |
| 126 » » aid = parts[1] |
| 127 » } |
| 128 |
| 129 » memctx := newMemContext(fqAppID) |
| 123 c = context.WithValue(c, memContextKey, memctx) | 130 c = context.WithValue(c, memContextKey, memctx) |
| 124 c = context.WithValue(c, memContextNoTxnKey, memctx) | 131 c = context.WithValue(c, memContextNoTxnKey, memctx) |
| 125 c = useGID(c, func(mod *globalInfoData) { | 132 c = useGID(c, func(mod *globalInfoData) { |
| 126 » » mod.appid = aid | 133 » » mod.appID = aid |
| 134 » » mod.fqAppID = fqAppID |
| 127 }) | 135 }) |
| 128 return useMod(useMail(useUser(useTQ(useRDS(useMC(useGI(c))))))) | 136 return useMod(useMail(useUser(useTQ(useRDS(useMC(useGI(c))))))) |
| 129 } | 137 } |
| 130 | 138 |
| 131 func cur(c context.Context) (p *memContext) { | 139 func cur(c context.Context) (p *memContext) { |
| 132 p, _ = c.Value(memContextKey).(*memContext) | 140 p, _ = c.Value(memContextKey).(*memContext) |
| 133 return | 141 return |
| 134 } | 142 } |
| 135 | 143 |
| 136 func curNoTxn(c context.Context) (p *memContext) { | 144 func curNoTxn(c context.Context) (p *memContext) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 if o != nil && o.Attempts != 0 { | 205 if o != nil && o.Attempts != 0 { |
| 198 attempts = o.Attempts | 206 attempts = o.Attempts |
| 199 } | 207 } |
| 200 for attempt := 0; attempt < attempts; attempt++ { | 208 for attempt := 0; attempt < attempts; attempt++ { |
| 201 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er
rConcurrentTransaction { | 209 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er
rConcurrentTransaction { |
| 202 return err | 210 return err |
| 203 } | 211 } |
| 204 } | 212 } |
| 205 return ds.ErrConcurrentTransaction | 213 return ds.ErrConcurrentTransaction |
| 206 } | 214 } |
| OLD | NEW |