add clear cache

This commit is contained in:
mantaohuang 2020-04-18 00:31:12 -04:00
parent b0b783f7c8
commit d8ac7b9b2e
3 changed files with 25 additions and 19 deletions

View File

@ -144,7 +144,8 @@ func NewPM(confFp string) (*ProxyManager, error) {
pm.Cache = NewQueueMap()
pm.applyWeight()
// go pm.keepClearingCache()
go pm.dynamicWeightLoader()
pm.dynamicWeightLoader()
pm.listenToClearCache()
return &pm, nil
}
func (pm *ProxyManager) DescribeLoadBalanceMode() string {
@ -227,24 +228,16 @@ func (pm *ProxyManager) Get(addr string) (string, bool) {
}
}
// func (pm *ProxyManager) ClearCache() {
// // fmt.Println("clearing cache")
// pm.mux.Lock()
// pm.Cache = New()
// pm.mux.Unlock()
// // fmt.Println("after:", pm.Cache)
// }
// func (pm *ProxyManager) keepClearingCache() {
// interval := pm.CacheCleanInterval
// if interval <= 0 {
// return
// }
// for {
// time.Sleep(time.Duration(interval) * time.Second)
// pm.ClearCache()
// }
// }
func (pm *ProxyManager) listenToClearCache() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGUSR2, syscall.SIGTERM)
go func() {
<-sigs
pm.mux.Lock()
pm.Cache = NewQueueMap()
pm.mux.Unlock()
}()
}
func (pm *ProxyManager) dynamicWeightLoader() {
sigs := make(chan os.Signal, 1)

View File

@ -195,6 +195,13 @@ class OManager:
# else:
#print("weight not changed, weights:", new_weights)
def clear_cache(self):
for pid in self.pids:
try:
os.kill(pid, signal.SIGUSR2)
except:
print("error clear cache of", pid)
async def run_cmd(self, cmd):
while True:
print("Manager trying to start go")

View File

@ -95,6 +95,11 @@ class RemoveInstatnceHandler(tornado.web.RequestHandler):
self.write("remove all")
class ClearCacheInstatnceHandler(tornado.web.RequestHandler):
def get(self):
om.clear_cache()
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
@ -103,6 +108,7 @@ def make_app():
(r"/start", StartInstatnceHandler),
(r"/stop", StopInstatnceHandler),
(r"/remove", RemoveInstatnceHandler),
(r"/clear_cache", ClearCacheInstatnceHandler),
])