| 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 prod | 5 package prod |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "net/http" | 8 "net/http" |
| 9 | 9 |
| 10 "github.com/luci/gae/service/info" | 10 "github.com/luci/gae/service/info" |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 "google.golang.org/appengine" | 12 "google.golang.org/appengine" |
| 13 ) | 13 ) |
| 14 | 14 |
| 15 type key int | 15 type key int |
| 16 | 16 |
| 17 var ( | 17 var ( |
| 18 prodContextKey key | 18 prodContextKey key |
| 19 prodContextNoTxnKey key = 1 | 19 prodContextNoTxnKey key = 1 |
| 20 probeCacheKey key = 2 | 20 probeCacheKey key = 2 |
| 21 ) | 21 ) |
| 22 | 22 |
| 23 // AEContext retrieves the raw "google.golang.org/appengine" compatible Context. | 23 // AEContext retrieves the raw "google.golang.org/appengine" compatible Context. |
| 24 // |
| 25 // It also transfers deadline of `c` to AE context, since deadline is used for |
| 26 // RPCs. Doesn't transfer cancelation ability though (since it's ignored by GAE |
| 27 // anyway). |
| 24 func AEContext(c context.Context) context.Context { | 28 func AEContext(c context.Context) context.Context { |
| 25 aeCtx, _ := c.Value(prodContextKey).(context.Context) | 29 aeCtx, _ := c.Value(prodContextKey).(context.Context) |
| 30 if deadline, ok := c.Deadline(); ok { |
| 31 aeCtx, _ = context.WithDeadline(aeCtx, deadline) |
| 32 } |
| 26 return aeCtx | 33 return aeCtx |
| 27 } | 34 } |
| 28 | 35 |
| 29 // AEContextNoTxn retrieves the raw "google.golang.org/appengine" compatible | 36 // AEContextNoTxn retrieves the raw "google.golang.org/appengine" compatible |
| 30 // Context that's not part of a transaction. | 37 // Context that's not part of a transaction. |
| 31 func AEContextNoTxn(c context.Context) context.Context { | 38 func AEContextNoTxn(c context.Context) context.Context { |
| 32 aeCtx, _ := c.Value(prodContextNoTxnKey).(context.Context) | 39 aeCtx, _ := c.Value(prodContextNoTxnKey).(context.Context) |
| 33 aeCtx, err := appengine.Namespace(aeCtx, info.Get(c).GetNamespace()) | 40 aeCtx, err := appengine.Namespace(aeCtx, info.Get(c).GetNamespace()) |
| 34 if err != nil { | 41 if err != nil { |
| 35 panic(err) | 42 panic(err) |
| 36 } | 43 } |
| 44 if deadline, ok := c.Deadline(); ok { |
| 45 aeCtx, _ = context.WithDeadline(aeCtx, deadline) |
| 46 } |
| 37 return aeCtx | 47 return aeCtx |
| 38 } | 48 } |
| 39 | 49 |
| 40 // Use adds production implementations for all the gae services to the | 50 // Use adds production implementations for all the gae services to the |
| 41 // context. | 51 // context. |
| 42 // | 52 // |
| 43 // The services added are: | 53 // The services added are: |
| 44 // - github.com/luci/gae/service/datastore | 54 // - github.com/luci/gae/service/datastore |
| 45 // - github.com/luci/gae/service/taskqueue | 55 // - github.com/luci/gae/service/taskqueue |
| 46 // - github.com/luci/gae/service/memcache | 56 // - github.com/luci/gae/service/memcache |
| 47 // - github.com/luci/gae/service/info | 57 // - github.com/luci/gae/service/info |
| 48 // - github.com/luci/gae/service/urlfetch | 58 // - github.com/luci/gae/service/urlfetch |
| 49 // - github.com/luci/gae/service/user | 59 // - github.com/luci/gae/service/user |
| 50 // - github.com/luci-go/common/logging | 60 // - github.com/luci-go/common/logging |
| 51 // | 61 // |
| 52 // These can be retrieved with the <service>.Get functions. | 62 // These can be retrieved with the <service>.Get functions. |
| 53 // | 63 // |
| 54 // The implementations are all backed by the real appengine SDK functionality, | 64 // The implementations are all backed by the real appengine SDK functionality, |
| 55 func Use(c context.Context, r *http.Request) context.Context { | 65 func Use(c context.Context, r *http.Request) context.Context { |
| 56 aeCtx := appengine.NewContext(r) | 66 aeCtx := appengine.NewContext(r) |
| 57 c = context.WithValue(c, prodContextKey, aeCtx) | 67 c = context.WithValue(c, prodContextKey, aeCtx) |
| 58 c = context.WithValue(c, prodContextNoTxnKey, aeCtx) | 68 c = context.WithValue(c, prodContextNoTxnKey, aeCtx) |
| 59 return useUser(useURLFetch(useRDS(useMC(useTQ(useGI(useLogging(c))))))) | 69 return useUser(useURLFetch(useRDS(useMC(useTQ(useGI(useLogging(c))))))) |
| 60 } | 70 } |
| OLD | NEW |