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 "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 ) | 14 ) |
14 | 15 |
15 type key int | 16 type key int |
16 | 17 |
17 var namespaceCopyKey key | 18 var probeCacheKey key |
18 | 19 |
19 // useGI adds a gae.GlobalInfo implementation to context, accessible | 20 // useGI adds a gae.GlobalInfo implementation to context, accessible |
20 // by gae.GetGI(c) | 21 // by gae.GetGI(c) |
21 func useGI(c context.Context) context.Context { | 22 func useGI(c context.Context) context.Context { |
22 » // TODO(iannucci): make a better way to get the initial namespace? | 23 » probeCache := getProbeCache(c) |
23 » c = context.WithValue(c, namespaceCopyKey, "") | 24 » if probeCache == nil { |
25 » » c = withProbeCache(c, probe(c)) | |
26 » } | |
27 | |
24 return info.SetFactory(c, func(ci context.Context) info.Interface { | 28 return info.SetFactory(c, func(ci context.Context) info.Interface { |
25 return giImpl{ci} | 29 return giImpl{ci} |
26 }) | 30 }) |
27 } | 31 } |
28 | 32 |
29 type giImpl struct{ context.Context } | 33 type giImpl struct{ context.Context } |
30 | 34 |
31 func (g giImpl) AccessToken(scopes ...string) (token string, expiry time.Time, e rr error) { | 35 func (g giImpl) AccessToken(scopes ...string) (token string, expiry time.Time, e rr error) { |
32 return appengine.AccessToken(g, scopes...) | 36 return appengine.AccessToken(g, scopes...) |
33 } | 37 } |
34 func (g giImpl) AppID() string { | 38 func (g giImpl) AppID() string { |
35 return appengine.AppID(g) | 39 return appengine.AppID(g) |
36 } | 40 } |
41 func (g giImpl) FullyQualifiedAppID() string { | |
42 return getProbeCache(g).fqaid | |
43 } | |
37 func (g giImpl) GetNamespace() string { | 44 func (g giImpl) GetNamespace() string { |
38 » return g.Value(namespaceCopyKey).(string) | 45 » return getProbeCache(g).namespace |
39 } | 46 } |
40 func (g giImpl) Datacenter() string { | 47 func (g giImpl) Datacenter() string { |
41 return appengine.Datacenter(g) | 48 return appengine.Datacenter(g) |
42 } | 49 } |
43 func (g giImpl) DefaultVersionHostname() string { | 50 func (g giImpl) DefaultVersionHostname() string { |
44 return appengine.DefaultVersionHostname(g) | 51 return appengine.DefaultVersionHostname(g) |
45 } | 52 } |
46 func (g giImpl) InstanceID() string { | 53 func (g giImpl) InstanceID() string { |
47 return appengine.InstanceID() | 54 return appengine.InstanceID() |
48 } | 55 } |
(...skipping 10 matching lines...) Expand all Loading... | |
59 return appengine.ModuleHostname(g, module, version, instance) | 66 return appengine.ModuleHostname(g, module, version, instance) |
60 } | 67 } |
61 func (g giImpl) ModuleName() (name string) { | 68 func (g giImpl) ModuleName() (name string) { |
62 return appengine.ModuleName(g) | 69 return appengine.ModuleName(g) |
63 } | 70 } |
64 func (g giImpl) Namespace(namespace string) (context.Context, error) { | 71 func (g giImpl) Namespace(namespace string) (context.Context, error) { |
65 c, err := appengine.Namespace(g, namespace) | 72 c, err := appengine.Namespace(g, namespace) |
66 if err != nil { | 73 if err != nil { |
67 return c, err | 74 return c, err |
68 } | 75 } |
69 » return context.WithValue(c, namespaceCopyKey, namespace), nil | 76 » pc := *getProbeCache(g) |
77 » pc.namespace = namespace | |
78 » return withProbeCache(c, &pc), nil | |
70 } | 79 } |
71 func (g giImpl) PublicCertificates() ([]info.Certificate, error) { | 80 func (g giImpl) PublicCertificates() ([]info.Certificate, error) { |
72 certs, err := appengine.PublicCertificates(g) | 81 certs, err := appengine.PublicCertificates(g) |
73 if err != nil { | 82 if err != nil { |
74 return nil, err | 83 return nil, err |
75 } | 84 } |
76 ret := make([]info.Certificate, len(certs)) | 85 ret := make([]info.Certificate, len(certs)) |
77 for i, c := range certs { | 86 for i, c := range certs { |
78 ret[i] = info.Certificate(c) | 87 ret[i] = info.Certificate(c) |
79 } | 88 } |
80 return ret, nil | 89 return ret, nil |
81 } | 90 } |
82 func (g giImpl) RequestID() string { | 91 func (g giImpl) RequestID() string { |
83 return appengine.RequestID(g) | 92 return appengine.RequestID(g) |
84 } | 93 } |
85 func (g giImpl) ServerSoftware() string { | 94 func (g giImpl) ServerSoftware() string { |
86 return appengine.ServerSoftware() | 95 return appengine.ServerSoftware() |
87 } | 96 } |
88 func (g giImpl) ServiceAccount() (string, error) { | 97 func (g giImpl) ServiceAccount() (string, error) { |
89 return appengine.ServiceAccount(g) | 98 return appengine.ServiceAccount(g) |
90 } | 99 } |
91 func (g giImpl) SignBytes(bytes []byte) (keyName string, signature []byte, err e rror) { | 100 func (g giImpl) SignBytes(bytes []byte) (keyName string, signature []byte, err e rror) { |
92 return appengine.SignBytes(g, bytes) | 101 return appengine.SignBytes(g, bytes) |
93 } | 102 } |
94 func (g giImpl) VersionID() string { | 103 func (g giImpl) VersionID() string { |
95 return appengine.VersionID(g) | 104 return appengine.VersionID(g) |
96 } | 105 } |
106 | |
107 type infoProbeCache struct { | |
108 namespace string | |
109 fqaid string | |
110 } | |
111 | |
112 func probe(c context.Context) *infoProbeCache { | |
113 probeKey := datastore.NewKey(c, "", "", 0, nil) | |
iannucci
2015/08/17 21:17:39
I would make this a valid-looking key, just in cas
dnj
2015/08/17 21:22:54
Done.
| |
114 return &infoProbeCache{ | |
115 namespace: probeKey.Namespace(), | |
116 fqaid: probeKey.AppID(), | |
117 } | |
118 } | |
119 | |
120 func getProbeCache(c context.Context) *infoProbeCache { | |
121 if pc, ok := c.Value(probeCacheKey).(*infoProbeCache); ok { | |
122 return pc | |
123 } | |
124 return nil | |
125 } | |
126 | |
127 func withProbeCache(c context.Context, pc *infoProbeCache) context.Context { | |
128 return context.WithValue(c, probeCacheKey, pc) | |
129 } | |
OLD | NEW |