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 dscache | 5 package dscache |
6 | 6 |
7 import ( | 7 import ( |
8 "sync" | 8 "sync" |
9 "time" | 9 "time" |
10 | 10 |
11 "github.com/luci/gae/service/datastore" | 11 "github.com/luci/gae/service/datastore" |
12 "github.com/luci/gae/service/info" | |
13 "github.com/luci/gae/service/memcache" | 12 "github.com/luci/gae/service/memcache" |
14 "github.com/luci/luci-go/common/clock" | 13 "github.com/luci/luci-go/common/clock" |
15 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
16 ) | 15 ) |
17 | 16 |
18 // GlobalConfig is the entity definition for dscache's global configuration. | 17 // GlobalConfig is the entity definition for dscache's global configuration. |
19 // | 18 // |
20 // It's Enable field can be set to false to cause all dscache operations | 19 // It's Enable field can be set to false to cause all dscache operations |
21 // (read and write) to cease in a given application. | 20 // (read and write) to cease in a given application. |
22 // | 21 // |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 } | 66 } |
68 | 67 |
69 globalEnabledLock.Lock() | 68 globalEnabledLock.Lock() |
70 defer globalEnabledLock.Unlock() | 69 defer globalEnabledLock.Unlock() |
71 // just in case we raced | 70 // just in case we raced |
72 if now.Before(globalEnabledNextCheck) { | 71 if now.Before(globalEnabledNextCheck) { |
73 return globalEnabled | 72 return globalEnabled |
74 } | 73 } |
75 | 74 |
76 // always go to the default namespace | 75 // always go to the default namespace |
77 c, err := info.Get(c).Namespace("") | |
78 if err != nil { | |
79 return true | |
80 } | |
81 cfg := &GlobalConfig{Enable: true} | 76 cfg := &GlobalConfig{Enable: true} |
82 if err := datastore.Get(c).Get(cfg); err != nil && err != datastore.ErrN
oSuchEntity { | 77 if err := datastore.Get(c).Get(cfg); err != nil && err != datastore.ErrN
oSuchEntity { |
83 return true | 78 return true |
84 } | 79 } |
85 globalEnabled = cfg.Enable | 80 globalEnabled = cfg.Enable |
86 globalEnabledNextCheck = now.Add(GlobalEnabledCheckInterval) | 81 globalEnabledNextCheck = now.Add(GlobalEnabledCheckInterval) |
87 return globalEnabled | 82 return globalEnabled |
88 } | 83 } |
89 | 84 |
90 // SetGlobalEnable is a convenience function for manipulating the GlobalConfig. | 85 // SetGlobalEnable is a convenience function for manipulating the GlobalConfig. |
91 // | 86 // |
92 // It's meant to be called from admin handlers on your app to turn dscache | 87 // It's meant to be called from admin handlers on your app to turn dscache |
93 // functionality on or off in emergencies. | 88 // functionality on or off in emergencies. |
94 func SetGlobalEnable(c context.Context, memcacheEnabled bool) error { | 89 func SetGlobalEnable(c context.Context, memcacheEnabled bool) error { |
95 // always go to the default namespace | |
96 c, err := info.Get(c).Namespace("") | |
97 if err != nil { | |
98 return err | |
99 } | |
100 return datastore.Get(c).RunInTransaction(func(c context.Context) error { | 90 return datastore.Get(c).RunInTransaction(func(c context.Context) error { |
101 ds := datastore.Get(c) | 91 ds := datastore.Get(c) |
102 cfg := &GlobalConfig{Enable: true} | 92 cfg := &GlobalConfig{Enable: true} |
103 if err := ds.Get(cfg); err != nil && err != datastore.ErrNoSuchE
ntity { | 93 if err := ds.Get(cfg); err != nil && err != datastore.ErrNoSuchE
ntity { |
104 return err | 94 return err |
105 } | 95 } |
106 if cfg.Enable == memcacheEnabled { | 96 if cfg.Enable == memcacheEnabled { |
107 return nil | 97 return nil |
108 } | 98 } |
109 cfg.Enable = memcacheEnabled | 99 cfg.Enable = memcacheEnabled |
110 if memcacheEnabled { | 100 if memcacheEnabled { |
111 // when going false -> true, wipe memcache. | 101 // when going false -> true, wipe memcache. |
112 if err := memcache.Get(c).Flush(); err != nil { | 102 if err := memcache.Get(c).Flush(); err != nil { |
113 return err | 103 return err |
114 } | 104 } |
115 } | 105 } |
116 return ds.Put(cfg) | 106 return ds.Put(cfg) |
117 }, nil) | 107 }, nil) |
118 } | 108 } |
OLD | NEW |