diff --git a/go-socks-lb/manager.go b/go-socks-lb/manager.go index 174ff2e..ce4e107 100644 --- a/go-socks-lb/manager.go +++ b/go-socks-lb/manager.go @@ -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) diff --git a/o_manager.py b/o_manager.py index c68e294..20c102b 100644 --- a/o_manager.py +++ b/o_manager.py @@ -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") diff --git a/test.py b/test.py index 5200d25..813a294 100644 --- a/test.py +++ b/test.py @@ -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), ])