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" | 12 "github.com/luci/gae/service/info" |
13 "github.com/luci/gae/service/memcache" | 13 "github.com/luci/gae/service/memcache" |
14 "github.com/luci/luci-go/common/clock" | 14 "github.com/luci/luci-go/common/clock" |
15 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
16 ) | 16 ) |
17 | 17 |
| 18 // GlobalConfig is the entity definition for dscache's global configuration. |
| 19 // |
| 20 // It's Enable field can be set to false to cause all dscache operations |
| 21 // (read and write) to cease in a given application. |
| 22 // |
| 23 // This should be manipulated in the GLOBAL (e.g. empty) namespace only. When |
| 24 // written there, it affects activity in all namespaces. |
18 type GlobalConfig struct { | 25 type GlobalConfig struct { |
19 _id int64 `gae:"$id,1"` | 26 _id int64 `gae:"$id,1"` |
20 _kind string `gae:"$kind,dscache"` | 27 _kind string `gae:"$kind,dscache"` |
21 | 28 |
22 Enable bool | 29 Enable bool |
23 } | 30 } |
24 | 31 |
25 var ( | 32 var ( |
26 globalEnabledLock = sync.RWMutex{} | 33 globalEnabledLock = sync.RWMutex{} |
27 | 34 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 } | 80 } |
74 cfg := &GlobalConfig{Enable: true} | 81 cfg := &GlobalConfig{Enable: true} |
75 if err := datastore.Get(c).Get(cfg); err != nil && err != datastore.ErrN
oSuchEntity { | 82 if err := datastore.Get(c).Get(cfg); err != nil && err != datastore.ErrN
oSuchEntity { |
76 return true | 83 return true |
77 } | 84 } |
78 globalEnabled = cfg.Enable | 85 globalEnabled = cfg.Enable |
79 globalEnabledNextCheck = now.Add(GlobalEnabledCheckInterval) | 86 globalEnabledNextCheck = now.Add(GlobalEnabledCheckInterval) |
80 return globalEnabled | 87 return globalEnabled |
81 } | 88 } |
82 | 89 |
| 90 // SetGlobalEnable is a convenience function for manipulating the GlobalConfig. |
| 91 // |
| 92 // It's meant to be called from admin handlers on your app to turn dscache |
| 93 // functionality on or off in emergencies. |
83 func SetGlobalEnable(c context.Context, memcacheEnabled bool) error { | 94 func SetGlobalEnable(c context.Context, memcacheEnabled bool) error { |
84 // always go to the default namespace | 95 // always go to the default namespace |
85 c, err := info.Get(c).Namespace("") | 96 c, err := info.Get(c).Namespace("") |
86 if err != nil { | 97 if err != nil { |
87 return err | 98 return err |
88 } | 99 } |
89 return datastore.Get(c).RunInTransaction(func(c context.Context) error { | 100 return datastore.Get(c).RunInTransaction(func(c context.Context) error { |
90 ds := datastore.Get(c) | 101 ds := datastore.Get(c) |
91 cfg := &GlobalConfig{Enable: true} | 102 cfg := &GlobalConfig{Enable: true} |
92 if err := ds.Get(cfg); err != nil && err != datastore.ErrNoSuchE
ntity { | 103 if err := ds.Get(cfg); err != nil && err != datastore.ErrNoSuchE
ntity { |
93 return err | 104 return err |
94 } | 105 } |
95 if cfg.Enable == memcacheEnabled { | 106 if cfg.Enable == memcacheEnabled { |
96 return nil | 107 return nil |
97 } | 108 } |
98 cfg.Enable = memcacheEnabled | 109 cfg.Enable = memcacheEnabled |
99 if memcacheEnabled { | 110 if memcacheEnabled { |
100 // when going false -> true, wipe memcache. | 111 // when going false -> true, wipe memcache. |
101 if err := memcache.Get(c).Flush(); err != nil { | 112 if err := memcache.Get(c).Flush(); err != nil { |
102 return err | 113 return err |
103 } | 114 } |
104 } | 115 } |
105 return ds.Put(cfg) | 116 return ds.Put(cfg) |
106 }, nil) | 117 }, nil) |
107 } | 118 } |
OLD | NEW |