Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1087)

Side by Side Diff: impl/memory/context.go

Issue 2492053003: Fix 'impl/memory' context key. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | impl/memory/datastore.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 // context. If 'aid' contains a "~" character, it will be treated as the 110 // 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 "~". 111 // fully-qualified App ID and the AppID will be the string following the "~".
112 // 112 //
113 // These can be retrieved with the gae.Get functions. 113 // These can be retrieved with the gae.Get functions.
114 // 114 //
115 // The implementations are all backed by an in-memory implementation, and start 115 // The implementations are all backed by an in-memory implementation, and start
116 // with an empty state. 116 // with an empty state.
117 // 117 //
118 // Using this more than once per context.Context will cause a panic. 118 // Using this more than once per context.Context will cause a panic.
119 func UseWithAppID(c context.Context, aid string) context.Context { 119 func UseWithAppID(c context.Context, aid string) context.Context {
120 » if c.Value(memContextKey) != nil { 120 » if c.Value(&memContextKey) != nil {
121 panic(errors.New("memory.Use: called twice on the same Context") ) 121 panic(errors.New("memory.Use: called twice on the same Context") )
122 } 122 }
123 c = memlogger.Use(c) 123 c = memlogger.Use(c)
124 124
125 fqAppID := aid 125 fqAppID := aid
126 if parts := strings.SplitN(fqAppID, "~", 2); len(parts) == 2 { 126 if parts := strings.SplitN(fqAppID, "~", 2); len(parts) == 2 {
127 aid = parts[1] 127 aid = parts[1]
128 } 128 }
129 129
130 memctx := newMemContext(fqAppID) 130 memctx := newMemContext(fqAppID)
131 » c = context.WithValue(c, memContextKey, memctx) 131 » c = context.WithValue(c, &memContextKey, memctx)
132 c = useGID(c, func(mod *globalInfoData) { 132 c = useGID(c, func(mod *globalInfoData) {
133 mod.appID = aid 133 mod.appID = aid
134 mod.fqAppID = fqAppID 134 mod.fqAppID = fqAppID
135 }) 135 })
136 return useMod(useMail(useUser(useTQ(useRDS(useMC(useGI(c))))))) 136 return useMod(useMail(useUser(useTQ(useRDS(useMC(useGI(c)))))))
137 } 137 }
138 138
139 func cur(c context.Context) (*memContext, bool) { 139 func cur(c context.Context) (*memContext, bool) {
140 » if txn := c.Value(currentTxnKey); txn != nil { 140 » if txn := c.Value(&currentTxnKey); txn != nil {
141 // We are in a Transaction. 141 // We are in a Transaction.
142 return txn.(*memContext), true 142 return txn.(*memContext), true
143 } 143 }
144 » return c.Value(memContextKey).(*memContext), false 144 » return c.Value(&memContextKey).(*memContext), false
145 } 145 }
146 146
147 type memContextKeyType int
148
149 var ( 147 var (
150 » memContextKey memContextKeyType 148 » memContextKey = "gae:memory:context"
151 » currentTxnKey = 1 149 » currentTxnKey = "gae:memory:currentTxn"
152 ) 150 )
153 151
154 // weird stuff 152 // weird stuff
155 153
156 // RunInTransaction is here because it's really a service-wide transaction, not 154 // RunInTransaction is here because it's really a service-wide transaction, not
157 // just in the datastore. TaskQueue behaves differently in a transaction in 155 // just in the datastore. TaskQueue behaves differently in a transaction in
158 // a couple ways, for example. 156 // a couple ways, for example.
159 // 157 //
160 // It really should have been appengine.Context.RunInTransaction(func(tc...)), 158 // It really should have been appengine.Context.RunInTransaction(func(tc...)),
161 // but because it's not, this method is on dsImpl instead to mirror the official 159 // but because it's not, this method is on dsImpl instead to mirror the official
(...skipping 17 matching lines...) Expand all
179 177
180 txnMC := curMC.mkTxn(o) 178 txnMC := curMC.mkTxn(o)
181 179
182 defer func() { 180 defer func() {
183 txnMC.Lock() 181 txnMC.Lock()
184 defer txnMC.Unlock() 182 defer txnMC.Unlock()
185 183
186 txnMC.endTxn() 184 txnMC.endTxn()
187 }() 185 }()
188 186
189 » » if err := f(context.WithValue(d, currentTxnKey, txnMC)); err != nil { 187 » » if err := f(context.WithValue(d, &currentTxnKey, txnMC)); err != nil {
190 return err 188 return err
191 } 189 }
192 190
193 txnMC.Lock() 191 txnMC.Lock()
194 defer txnMC.Unlock() 192 defer txnMC.Unlock()
195 193
196 if applyForReal && curMC.canApplyTxn(txnMC) { 194 if applyForReal && curMC.canApplyTxn(txnMC) {
197 curMC.applyTxn(d, txnMC) 195 curMC.applyTxn(d, txnMC)
198 } else { 196 } else {
199 return ds.ErrConcurrentTransaction 197 return ds.ErrConcurrentTransaction
200 } 198 }
201 return nil 199 return nil
202 } 200 }
203 201
204 // From GAE docs for TransactionOptions: "If omitted, it defaults to 3." 202 // From GAE docs for TransactionOptions: "If omitted, it defaults to 3."
205 attempts := 3 203 attempts := 3
206 if o != nil && o.Attempts != 0 { 204 if o != nil && o.Attempts != 0 {
207 attempts = o.Attempts 205 attempts = o.Attempts
208 } 206 }
209 for attempt := 0; attempt < attempts; attempt++ { 207 for attempt := 0; attempt < attempts; attempt++ {
210 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er rConcurrentTransaction { 208 if err := loopBody(attempt >= d.data.txnFakeRetry); err != ds.Er rConcurrentTransaction {
211 return err 209 return err
212 } 210 }
213 } 211 }
214 return ds.ErrConcurrentTransaction 212 return ds.ErrConcurrentTransaction
215 } 213 }
OLDNEW
« no previous file with comments | « no previous file | impl/memory/datastore.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698