From 91576d1e08b1e02a632f1f6a1bf5fd58eff3521c Mon Sep 17 00:00:00 2001 From: BreakWa11 Date: Fri, 3 Jun 2016 20:56:59 +0800 Subject: [PATCH] detect thread status add API params --- Config.py | 16 ++++++++++++++-- db_transfer.py | 18 +++++++++--------- server.py | 4 ++-- server_pool.py | 4 ++-- shadowsocks/tcprelay.py | 4 ++-- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Config.py b/Config.py index 752ebde..f98f204 100644 --- a/Config.py +++ b/Config.py @@ -1,12 +1,24 @@ -#Config +# Config +TRANSFER_MUL = 1.0 +API_INTERFACE = 'mysql' + +# Mysql MYSQL_HOST = 'mdss.mengsky.net' MYSQL_PORT = 3306 MYSQL_USER = 'ss' MYSQL_PASS = 'ss' MYSQL_DB = 'shadowsocks' -MYSQL_TRANSFER_MUL = 1.0 MYSQL_UPDATE_TIME = 60 +# API +API_URL = 'breakwa11.org' +API_PORT = 80 +API_PATH = '/mu/v2/' +API_TOKEN = 'abcdef' +API_NODE_ID = 'id001' +API_UPDATE_TIME = 60 + +# Manager (ignore this) MANAGE_PASS = 'ss233333333' #if you want manage in other server you should set this value to global ip MANAGE_BIND_IP = '127.0.0.1' diff --git a/db_transfer.py b/db_transfer.py index d5c6994..8b46503 100644 --- a/db_transfer.py +++ b/db_transfer.py @@ -39,16 +39,16 @@ class DbTransfer(object): continue elif last_transfer[id][0] <= curr_transfer[id][0] and \ last_transfer[id][1] <= curr_transfer[id][1]: - dt_transfer[id] = [int((curr_transfer[id][0] - last_transfer[id][0]) * Config.MYSQL_TRANSFER_MUL), - int((curr_transfer[id][1] - last_transfer[id][1]) * Config.MYSQL_TRANSFER_MUL)] + dt_transfer[id] = [int((curr_transfer[id][0] - last_transfer[id][0]) * Config.TRANSFER_MUL), + int((curr_transfer[id][1] - last_transfer[id][1]) * Config.TRANSFER_MUL)] else: - dt_transfer[id] = [int(curr_transfer[id][0] * Config.MYSQL_TRANSFER_MUL), - int(curr_transfer[id][1] * Config.MYSQL_TRANSFER_MUL)] + dt_transfer[id] = [int(curr_transfer[id][0] * Config.TRANSFER_MUL), + int(curr_transfer[id][1] * Config.TRANSFER_MUL)] else: if curr_transfer[id][0] == 0 and curr_transfer[id][1] == 0: continue - dt_transfer[id] = [int(curr_transfer[id][0] * Config.MYSQL_TRANSFER_MUL), - int(curr_transfer[id][1] * Config.MYSQL_TRANSFER_MUL)] + dt_transfer[id] = [int(curr_transfer[id][0] * Config.TRANSFER_MUL), + int(curr_transfer[id][1] * Config.TRANSFER_MUL)] query_head = 'UPDATE user' query_sub_when = '' @@ -166,10 +166,10 @@ class DbTransfer(object): def del_servers(): for port in ServerPool.get_instance().tcp_servers_pool.keys(): if ServerPool.get_instance().server_is_run(port) > 0: - ServerPool.get_instance().cb_del_server(port) + ServerPool.get_instance().cb_del_server(port) for port in ServerPool.get_instance().tcp_ipv6_servers_pool.keys(): if ServerPool.get_instance().server_is_run(port) > 0: - ServerPool.get_instance().cb_del_server(port) + ServerPool.get_instance().cb_del_server(port) @staticmethod def thread_db(): @@ -190,7 +190,7 @@ class DbTransfer(object): trace = traceback.format_exc() logging.error(trace) #logging.warn('db thread except:%s' % e) - if DbTransfer.get_instance().event.wait(Config.MYSQL_UPDATE_TIME): + if DbTransfer.get_instance().event.wait(Config.MYSQL_UPDATE_TIME) or not ServerPool.get_instance().thread.is_alive(): break except KeyboardInterrupt as e: pass diff --git a/server.py b/server.py index a28f026..db375ad 100644 --- a/server.py +++ b/server.py @@ -50,8 +50,8 @@ def main(): thread = MainThread() thread.start() try: - while True: - time.sleep(99999) + while thread.is_alive(): + time.sleep(10) except (KeyboardInterrupt, IOError, OSError) as e: import traceback traceback.print_exc() diff --git a/server_pool.py b/server_pool.py index cf200e1..bcfb731 100644 --- a/server_pool.py +++ b/server_pool.py @@ -64,8 +64,8 @@ class ServerPool(object): self.stat_counter = {} self.loop = eventloop.EventLoop() - thread = MainThread( (self.loop, self.dns_resolver, self.mgr) ) - thread.start() + self.thread = MainThread( (self.loop, self.dns_resolver, self.mgr) ) + self.thread.start() @staticmethod def get_instance(): diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py index 4fb0dd7..77331d8 100644 --- a/shadowsocks/tcprelay.py +++ b/shadowsocks/tcprelay.py @@ -170,7 +170,7 @@ class TCPRelayHandler(object): self.last_activity = 0 self._update_activity() self._server.add_connection(1) - self._server.stat_add(common.to_str(self._client_address[0]), 1) + self._server.stat_add(self._client_address[0], 1) def __hash__(self): # default __hash__ is id / 16 @@ -878,7 +878,7 @@ class TCPRelayHandler(object): self._dns_resolver.remove_callback(self._handle_dns_resolved) self._server.remove_handler(self) self._server.add_connection(-1) - self._server.stat_add(common.to_str(self._client_address[0]), -1) + self._server.stat_add(self._client_address[0], -1) class TCPRelay(object): def __init__(self, config, dns_resolver, is_local, stat_callback=None, stat_counter=None):