Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be 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 "strings" |
| 10 "sync" | 10 "sync" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 for i := range *m { | 89 for i := range *m { |
| 90 (*m)[i].applyTxn(c, txnCtx[i]) | 90 (*m)[i].applyTxn(c, txnCtx[i]) |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 | 93 |
| 94 // Use calls UseWithAppID with the appid of "app" | 94 // Use calls UseWithAppID with the appid of "app" |
| 95 func Use(c context.Context) context.Context { | 95 func Use(c context.Context) context.Context { |
| 96 return UseWithAppID(c, "dev~app") | 96 return UseWithAppID(c, "dev~app") |
| 97 } | 97 } |
| 98 | 98 |
| 99 // UseInfo adds an implementation for: | |
| 100 // * github.com/luci/gae/service/info | |
| 101 // The application id wil be set to 'aid', and will not be modifiable in this | |
| 102 // context. If 'aid' contains a "~" character, it will be treated as the | |
| 103 // fully-qualified App ID and the AppID will be the string following the "~". | |
| 104 func UseInfo(c context.Context, aid string) context.Context { | |
| 105 if c.Value(&memContextKey) != nil { | |
| 106 panic(errors.New("memory.Use: called twice on the same Context") ) | |
| 107 } | |
| 108 | |
| 109 fqAppID := aid | |
| 110 if parts := strings.SplitN(fqAppID, "~", 2); len(parts) == 2 { | |
| 111 aid = parts[1] | |
| 112 } | |
| 113 | |
| 114 memctx := newMemContext(fqAppID) | |
|
Vadim Sh.
2017/04/20 00:12:46
this thing has mentions of datastore and task queu
| |
| 115 c = context.WithValue(c, &memContextKey, memctx) | |
| 116 | |
| 117 return useGI(useGID(c, func(mod *globalInfoData) { | |
| 118 mod.appID = aid | |
| 119 mod.fqAppID = fqAppID | |
| 120 })) | |
| 121 } | |
| 122 | |
| 99 // UseWithAppID adds implementations for the following gae services to the | 123 // UseWithAppID adds implementations for the following gae services to the |
| 100 // context: | 124 // context: |
| 101 // * github.com/luci/gae/service/datastore | 125 // * github.com/luci/gae/service/datastore |
| 102 // * github.com/luci/gae/service/info | 126 // * github.com/luci/gae/service/info |
| 103 // * github.com/luci/gae/service/mail | 127 // * github.com/luci/gae/service/mail |
| 104 // * github.com/luci/gae/service/memcache | 128 // * github.com/luci/gae/service/memcache |
| 105 // * github.com/luci/gae/service/taskqueue | 129 // * github.com/luci/gae/service/taskqueue |
| 106 // * github.com/luci/gae/service/user | 130 // * github.com/luci/gae/service/user |
| 107 // * github.com/luci/luci-go/common/logger (using memlogger) | 131 // * github.com/luci/luci-go/common/logger (using memlogger) |
| 108 // | 132 // |
| 109 // The application id wil be set to 'aid', and will not be modifiable in this | 133 // The application id wil be set to 'aid', and will not be modifiable in this |
| 110 // context. If 'aid' contains a "~" character, it will be treated as the | 134 // context. If 'aid' contains a "~" character, it will be treated as the |
| 111 // fully-qualified App ID and the AppID will be the string following the "~". | 135 // fully-qualified App ID and the AppID will be the string following the "~". |
| 112 // | 136 // |
| 113 // These can be retrieved with the gae.Get functions. | 137 // These can be retrieved with the gae.Get functions. |
| 114 // | 138 // |
| 115 // The implementations are all backed by an in-memory implementation, and start | 139 // The implementations are all backed by an in-memory implementation, and start |
| 116 // with an empty state. | 140 // with an empty state. |
| 117 // | 141 // |
| 118 // Using this more than once per context.Context will cause a panic. | 142 // Using this more than once per context.Context will cause a panic. |
| 119 func UseWithAppID(c context.Context, aid string) context.Context { | 143 func UseWithAppID(c context.Context, aid string) context.Context { |
| 120 if c.Value(&memContextKey) != nil { | |
| 121 panic(errors.New("memory.Use: called twice on the same Context") ) | |
| 122 } | |
| 123 c = memlogger.Use(c) | 144 c = memlogger.Use(c) |
| 124 | 145 » c = UseInfo(c, aid) // Panics if UseWithAppID is called twice. |
| 125 » fqAppID := aid | 146 » return useMod(useMail(useUser(useTQ(useRDS(useMC(c)))))) |
| 126 » if parts := strings.SplitN(fqAppID, "~", 2); len(parts) == 2 { | |
| 127 » » aid = parts[1] | |
| 128 » } | |
| 129 | |
| 130 » memctx := newMemContext(fqAppID) | |
| 131 » c = context.WithValue(c, &memContextKey, memctx) | |
| 132 » c = useGID(c, func(mod *globalInfoData) { | |
| 133 » » mod.appID = aid | |
| 134 » » mod.fqAppID = fqAppID | |
| 135 » }) | |
| 136 » return useMod(useMail(useUser(useTQ(useRDS(useMC(useGI(c))))))) | |
| 137 } | 147 } |
| 138 | 148 |
| 139 func cur(c context.Context) (*memContext, bool) { | 149 func cur(c context.Context) (*memContext, bool) { |
| 140 if txn := c.Value(¤tTxnKey); txn != nil { | 150 if txn := c.Value(¤tTxnKey); txn != nil { |
| 141 // We are in a Transaction. | 151 // We are in a Transaction. |
| 142 return txn.(*memContext), true | 152 return txn.(*memContext), true |
| 143 } | 153 } |
| 144 return c.Value(&memContextKey).(*memContext), false | 154 return c.Value(&memContextKey).(*memContext), false |
| 145 } | 155 } |
| 146 | 156 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 if o != nil && o.Attempts != 0 { | 214 if o != nil && o.Attempts != 0 { |
| 205 attempts = o.Attempts | 215 attempts = o.Attempts |
| 206 } | 216 } |
| 207 for attempt := 0; attempt < attempts; attempt++ { | 217 for attempt := 0; attempt < attempts; attempt++ { |
| 208 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er rConcurrentTransaction { | 218 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er rConcurrentTransaction { |
| 209 return err | 219 return err |
| 210 } | 220 } |
| 211 } | 221 } |
| 212 return ds.ErrConcurrentTransaction | 222 return ds.ErrConcurrentTransaction |
| 213 } | 223 } |
| OLD | NEW |