| 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 prod | 5 package prod |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" | 8 "time" |
| 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 "google.golang.org/appengine/datastore" | 13 "google.golang.org/appengine/datastore" |
| 14 ) | 14 ) |
| 15 | 15 |
| 16 // useGI adds a gae.GlobalInfo implementation to context, accessible | 16 // useGI adds a gae.GlobalInfo implementation to context, accessible |
| 17 // by gae.GetGI(c) | 17 // by gae.GetGI(c) |
| 18 func useGI(usrCtx context.Context) context.Context { | 18 func useGI(usrCtx context.Context) context.Context { |
| 19 probeCache := getProbeCache(usrCtx) | 19 probeCache := getProbeCache(usrCtx) |
| 20 if probeCache == nil { | 20 if probeCache == nil { |
| 21 usrCtx = withProbeCache(usrCtx, probe(AEContext(usrCtx))) | 21 usrCtx = withProbeCache(usrCtx, probe(AEContext(usrCtx))) |
| 22 } | 22 } |
| 23 | 23 |
| 24 » return info.SetFactory(usrCtx, func(ci context.Context) info.Interface { | 24 » return info.SetFactory(usrCtx, func(ci context.Context) info.RawInterfac
e { |
| 25 return giImpl{ci, AEContext(ci)} | 25 return giImpl{ci, AEContext(ci)} |
| 26 }) | 26 }) |
| 27 } | 27 } |
| 28 | 28 |
| 29 type giImpl struct { | 29 type giImpl struct { |
| 30 usrCtx context.Context | 30 usrCtx context.Context |
| 31 aeCtx context.Context | 31 aeCtx context.Context |
| 32 } | 32 } |
| 33 | 33 |
| 34 func (g giImpl) AccessToken(scopes ...string) (token string, expiry time.Time, e
rr error) { | 34 func (g giImpl) AccessToken(scopes ...string) (token string, expiry time.Time, e
rr error) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 func (g giImpl) Namespace(namespace string) (context.Context, error) { | 74 func (g giImpl) Namespace(namespace string) (context.Context, error) { |
| 75 aeCtx, err := appengine.Namespace(g.aeCtx, namespace) | 75 aeCtx, err := appengine.Namespace(g.aeCtx, namespace) |
| 76 if err != nil { | 76 if err != nil { |
| 77 return g.usrCtx, err | 77 return g.usrCtx, err |
| 78 } | 78 } |
| 79 usrCtx := context.WithValue(g.usrCtx, prodContextKey, aeCtx) | 79 usrCtx := context.WithValue(g.usrCtx, prodContextKey, aeCtx) |
| 80 pc := *getProbeCache(usrCtx) | 80 pc := *getProbeCache(usrCtx) |
| 81 pc.namespace = &namespace | 81 pc.namespace = &namespace |
| 82 return withProbeCache(usrCtx, &pc), nil | 82 return withProbeCache(usrCtx, &pc), nil |
| 83 } | 83 } |
| 84 func (g giImpl) MustNamespace(ns string) context.Context { | |
| 85 ret, err := g.Namespace(ns) | |
| 86 if err != nil { | |
| 87 panic(err) | |
| 88 } | |
| 89 return ret | |
| 90 } | |
| 91 func (g giImpl) PublicCertificates() ([]info.Certificate, error) { | 84 func (g giImpl) PublicCertificates() ([]info.Certificate, error) { |
| 92 certs, err := appengine.PublicCertificates(g.aeCtx) | 85 certs, err := appengine.PublicCertificates(g.aeCtx) |
| 93 if err != nil { | 86 if err != nil { |
| 94 return nil, err | 87 return nil, err |
| 95 } | 88 } |
| 96 ret := make([]info.Certificate, len(certs)) | 89 ret := make([]info.Certificate, len(certs)) |
| 97 for i, c := range certs { | 90 for i, c := range certs { |
| 98 ret[i] = info.Certificate(c) | 91 ret[i] = info.Certificate(c) |
| 99 } | 92 } |
| 100 return ret, nil | 93 return ret, nil |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 func getProbeCache(c context.Context) *infoProbeCache { | 132 func getProbeCache(c context.Context) *infoProbeCache { |
| 140 if pc, ok := c.Value(probeCacheKey).(*infoProbeCache); ok { | 133 if pc, ok := c.Value(probeCacheKey).(*infoProbeCache); ok { |
| 141 return pc | 134 return pc |
| 142 } | 135 } |
| 143 return nil | 136 return nil |
| 144 } | 137 } |
| 145 | 138 |
| 146 func withProbeCache(c context.Context, pc *infoProbeCache) context.Context { | 139 func withProbeCache(c context.Context, pc *infoProbeCache) context.Context { |
| 147 return context.WithValue(c, probeCacheKey, pc) | 140 return context.WithValue(c, probeCacheKey, pc) |
| 148 } | 141 } |
| OLD | NEW |