| 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 "sync" | 9 "sync" |
| 10 | 10 |
| 11 ds "github.com/luci/gae/service/datastore" | 11 ds "github.com/luci/gae/service/datastore" |
| 12 "github.com/luci/luci-go/common/logging/memlogger" |
| 12 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 13 ) | 14 ) |
| 14 | 15 |
| 15 var serializationDeterministic = false | 16 var serializationDeterministic = false |
| 16 | 17 |
| 17 type memContextObj interface { | 18 type memContextObj interface { |
| 18 sync.Locker | 19 sync.Locker |
| 19 canApplyTxn(m memContextObj) bool | 20 canApplyTxn(m memContextObj) bool |
| 20 applyTxn(c context.Context, m memContextObj) | 21 applyTxn(c context.Context, m memContextObj) |
| 21 | 22 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 for i := range *m { | 87 for i := range *m { |
| 87 (*m)[i].applyTxn(c, txnCtx[i]) | 88 (*m)[i].applyTxn(c, txnCtx[i]) |
| 88 } | 89 } |
| 89 } | 90 } |
| 90 | 91 |
| 91 // Use calls UseWithAppID with the appid of "dev~app" | 92 // Use calls UseWithAppID with the appid of "dev~app" |
| 92 func Use(c context.Context) context.Context { | 93 func Use(c context.Context) context.Context { |
| 93 return UseWithAppID(c, "dev~app") | 94 return UseWithAppID(c, "dev~app") |
| 94 } | 95 } |
| 95 | 96 |
| 96 // UseWithAppID adds implementations for the following gae interfaces to the | 97 // UseWithAppID adds implementations for the following gae services to the |
| 97 // context: | 98 // context: |
| 98 // * gae.Datastore | 99 // * github.com/luci/gae/service/datastore |
| 99 // * gae.TaskQueue | 100 // * github.com/luci/gae/service/taskqueue |
| 100 // * gae.Memcache | 101 // * github.com/luci/gae/service/memcache |
| 101 // * gae.GlobalInfo | 102 // * github.com/luci/gae/service/info |
| 103 // * github.com/luci/gae/service/user |
| 104 // * github.com/luci/luci-go/common/logger (using memlogger) |
| 102 // | 105 // |
| 103 // The application id wil be set to 'aid', and will not be modifiable in this | 106 // The application id wil be set to 'aid', and will not be modifiable in this |
| 104 // context. | 107 // context. |
| 105 // | 108 // |
| 106 // These can be retrieved with the gae.Get functions. | 109 // These can be retrieved with the gae.Get functions. |
| 107 // | 110 // |
| 108 // The implementations are all backed by an in-memory implementation, and start | 111 // The implementations are all backed by an in-memory implementation, and start |
| 109 // with an empty state. | 112 // with an empty state. |
| 110 // | 113 // |
| 111 // Using this more than once per context.Context will cause a panic. | 114 // Using this more than once per context.Context will cause a panic. |
| 112 func UseWithAppID(c context.Context, aid string) context.Context { | 115 func UseWithAppID(c context.Context, aid string) context.Context { |
| 113 if c.Value(memContextKey) != nil { | 116 if c.Value(memContextKey) != nil { |
| 114 panic(errors.New("memory.Use: called twice on the same Context")
) | 117 panic(errors.New("memory.Use: called twice on the same Context")
) |
| 115 } | 118 } |
| 119 c = memlogger.Use(c) |
| 120 |
| 116 memctx := newMemContext(aid) | 121 memctx := newMemContext(aid) |
| 117 c = context.WithValue(c, memContextKey, memctx) | 122 c = context.WithValue(c, memContextKey, memctx) |
| 118 c = context.WithValue(c, memContextNoTxnKey, memctx) | 123 c = context.WithValue(c, memContextNoTxnKey, memctx) |
| 119 c = context.WithValue(c, giContextKey, &globalInfoData{appid: aid}) | 124 c = context.WithValue(c, giContextKey, &globalInfoData{appid: aid}) |
| 120 | |
| 121 return useTQ(useRDS(useMC(useGI(c, aid)))) | 125 return useTQ(useRDS(useMC(useGI(c, aid)))) |
| 122 } | 126 } |
| 123 | 127 |
| 124 func cur(c context.Context) (p *memContext) { | 128 func cur(c context.Context) (p *memContext) { |
| 125 p, _ = c.Value(memContextKey).(*memContext) | 129 p, _ = c.Value(memContextKey).(*memContext) |
| 126 return | 130 return |
| 127 } | 131 } |
| 128 | 132 |
| 129 func curNoTxn(c context.Context) (p *memContext) { | 133 func curNoTxn(c context.Context) (p *memContext) { |
| 130 p, _ = c.Value(memContextNoTxnKey).(*memContext) | 134 p, _ = c.Value(memContextNoTxnKey).(*memContext) |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 if o != nil && o.Attempts != 0 { | 194 if o != nil && o.Attempts != 0 { |
| 191 attempts = o.Attempts | 195 attempts = o.Attempts |
| 192 } | 196 } |
| 193 for attempt := 0; attempt < attempts; attempt++ { | 197 for attempt := 0; attempt < attempts; attempt++ { |
| 194 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er
rConcurrentTransaction { | 198 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er
rConcurrentTransaction { |
| 195 return err | 199 return err |
| 196 } | 200 } |
| 197 } | 201 } |
| 198 return ds.ErrConcurrentTransaction | 202 return ds.ErrConcurrentTransaction |
| 199 } | 203 } |
| OLD | NEW |