From 569aaa5248d37f512b46cc11323dee17c17044cc Mon Sep 17 00:00:00 2001 From: BreakWa11 Date: Wed, 8 Jun 2016 16:15:26 +0800 Subject: [PATCH] hot load config support userapiconfig --- Config.py => apiconfig.py | 6 +++--- config.json | 1 - configloader.py | 27 +++++++++++++++++++++++++++ db_transfer.py | 26 +++++++++++++------------- logrun.sh | 6 ++++++ run.sh | 2 +- server_pool.py | 13 +++++-------- shadowsocks/logrun.sh | 6 ++++++ shadowsocks/run.sh | 3 ++- 9 files changed, 63 insertions(+), 27 deletions(-) rename Config.py => apiconfig.py (84%) create mode 100644 configloader.py create mode 100644 logrun.sh create mode 100644 shadowsocks/logrun.sh diff --git a/Config.py b/apiconfig.py similarity index 84% rename from Config.py rename to apiconfig.py index d3f2120..580e55b 100644 --- a/Config.py +++ b/apiconfig.py @@ -1,6 +1,7 @@ # Config TRANSFER_MUL = 1.0 -API_INTERFACE = 'mysql' +NODE_ID = 1 +API_INTERFACE = 'sspanelv2' #sspanelv2, muapiv2 # Mysql MYSQL_HOST = 'mdss.mengsky.net' @@ -11,11 +12,10 @@ MYSQL_DB = 'shadowsocks' MYSQL_UPDATE_TIME = 60 # API -API_HOST = 'breakwa11.org' +API_HOST = 'breakwa11.moe' API_PORT = 80 API_PATH = '/mu/v2/' API_TOKEN = 'abcdef' -API_NODE_ID = 'id001' API_UPDATE_TIME = 60 # Manager (ignore this) diff --git a/config.json b/config.json index 30bc51d..8f7a4c9 100644 --- a/config.json +++ b/config.json @@ -16,5 +16,4 @@ "connect_verbose_info": 0, "redirect": "", "fast_open": false, - "workers": 1 } diff --git a/configloader.py b/configloader.py new file mode 100644 index 0000000..8d0ec0d --- /dev/null +++ b/configloader.py @@ -0,0 +1,27 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- + +config = None + +def load_config(): + global config + try: + import userapiconfig + reload(userapiconfig) + config = userapiconfig + return + except: + pass + try: + import apiconfig + reload(apiconfig) + config = apiconfig + except: + pass + +def get_config(): + global config + return config + +load_config() + diff --git a/db_transfer.py b/db_transfer.py index 8b46503..b64eb65 100644 --- a/db_transfer.py +++ b/db_transfer.py @@ -6,9 +6,9 @@ import cymysql import time import sys from server_pool import ServerPool -import Config import traceback from shadowsocks import common +from configloader import load_config, get_config class DbTransfer(object): @@ -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.TRANSFER_MUL), - int((curr_transfer[id][1] - last_transfer[id][1]) * Config.TRANSFER_MUL)] + dt_transfer[id] = [int((curr_transfer[id][0] - last_transfer[id][0]) * get_config().TRANSFER_MUL), + int((curr_transfer[id][1] - last_transfer[id][1]) * get_config().TRANSFER_MUL)] else: - dt_transfer[id] = [int(curr_transfer[id][0] * Config.TRANSFER_MUL), - int(curr_transfer[id][1] * Config.TRANSFER_MUL)] + dt_transfer[id] = [int(curr_transfer[id][0] * get_config().TRANSFER_MUL), + int(curr_transfer[id][1] * get_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.TRANSFER_MUL), - int(curr_transfer[id][1] * Config.TRANSFER_MUL)] + dt_transfer[id] = [int(curr_transfer[id][0] * get_config().TRANSFER_MUL), + int(curr_transfer[id][1] * get_config().TRANSFER_MUL)] query_head = 'UPDATE user' query_sub_when = '' @@ -71,8 +71,8 @@ class DbTransfer(object): ' END, t = ' + str(int(last_time)) + \ ' WHERE port IN (%s)' % query_sub_in #print query_sql - conn = cymysql.connect(host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER, - passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8') + conn = cymysql.connect(host=get_config().MYSQL_HOST, port=get_config().MYSQL_PORT, user=get_config().MYSQL_USER, + passwd=get_config().MYSQL_PASS, db=get_config().MYSQL_DB, charset='utf8') cur = conn.cursor() cur.execute(query_sql) cur.close() @@ -90,8 +90,8 @@ class DbTransfer(object): except Exception as e: keys = ['port', 'u', 'd', 'transfer_enable', 'passwd', 'enable' ] reload(cymysql) - conn = cymysql.connect(host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER, - passwd=Config.MYSQL_PASS, db=Config.MYSQL_DB, charset='utf8') + conn = cymysql.connect(host=get_config().MYSQL_HOST, port=get_config().MYSQL_PORT, user=get_config().MYSQL_USER, + passwd=get_config().MYSQL_PASS, db=get_config().MYSQL_DB, charset='utf8') cur = conn.cursor() cur.execute("SELECT " + ','.join(keys) + " FROM user") rows = [] @@ -180,7 +180,7 @@ class DbTransfer(object): last_rows = [] try: while True: - reload(Config) + load_config() try: DbTransfer.get_instance().push_db_all_user() rows = DbTransfer.get_instance().pull_db_all_user() @@ -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) or not ServerPool.get_instance().thread.is_alive(): + if DbTransfer.get_instance().event.wait(get_config().MYSQL_UPDATE_TIME) or not ServerPool.get_instance().thread.is_alive(): break except KeyboardInterrupt as e: pass diff --git a/logrun.sh b/logrun.sh new file mode 100644 index 0000000..82a2105 --- /dev/null +++ b/logrun.sh @@ -0,0 +1,6 @@ +#!/bin/bash +cd `dirname $0` +eval $(ps -ef | grep "[0-9] python server\\.py m" | awk '{print "kill "$2}') +ulimit -n 512000 +nohup python server.py m>> ssserver.log 2>&1 & + diff --git a/run.sh b/run.sh index 82a2105..89f7b05 100644 --- a/run.sh +++ b/run.sh @@ -2,5 +2,5 @@ cd `dirname $0` eval $(ps -ef | grep "[0-9] python server\\.py m" | awk '{print "kill "$2}') ulimit -n 512000 -nohup python server.py m>> ssserver.log 2>&1 & +nohup python server.py m>> /dev/null 2>&1 & diff --git a/server_pool.py b/server_pool.py index bcfb731..b897d20 100644 --- a/server_pool.py +++ b/server_pool.py @@ -24,16 +24,11 @@ import os import logging import time -from shadowsocks import shell -from shadowsocks import eventloop -from shadowsocks import tcprelay -from shadowsocks import udprelay -from shadowsocks import asyncdns +from shadowsocks import shell, eventloop, tcprelay, udprelay, asyncdns import threading import sys -import asyncmgr -import Config from socket import * +from configloader import load_config, get_config class MainThread(threading.Thread): def __init__(self, params): @@ -127,6 +122,7 @@ class ServerPool(object): a_config['server'] = a_config['server_ipv6'] a_config['server_port'] = port a_config['password'] = password + a_config['max_connect'] = 128 try: logging.info("starting server at [%s]:%d" % (a_config['server'], port)) @@ -151,6 +147,7 @@ class ServerPool(object): a_config = self.config.copy() a_config['server_port'] = port a_config['password'] = password + a_config['max_connect'] = 128 try: logging.info("starting server at %s:%d" % (a_config['server'], port)) @@ -173,7 +170,7 @@ class ServerPool(object): logging.info("del server at %d" % port) try: udpsock = socket(AF_INET, SOCK_DGRAM) - udpsock.sendto('%s:%s:0:0' % (Config.MANAGE_PASS, port), (Config.MANAGE_BIND_IP, Config.MANAGE_PORT)) + udpsock.sendto('%s:%s:0:0' % (get_config().MANAGE_PASS, port), (get_config().MANAGE_BIND_IP, get_config().MANAGE_PORT)) udpsock.close() except Exception as e: logging.warn(e) diff --git a/shadowsocks/logrun.sh b/shadowsocks/logrun.sh new file mode 100644 index 0000000..2b7a5bd --- /dev/null +++ b/shadowsocks/logrun.sh @@ -0,0 +1,6 @@ +#!/bin/bash +cd `dirname $0` +eval $(ps -ef | grep "[0-9] python server\\.py a" | awk '{print "kill "$2}') +ulimit -n 4096 +nohup python server.py a >> ssserver.log 2>&1 & + diff --git a/shadowsocks/run.sh b/shadowsocks/run.sh index 497ceb8..7a91779 100644 --- a/shadowsocks/run.sh +++ b/shadowsocks/run.sh @@ -1,5 +1,6 @@ #!/bin/bash cd `dirname $0` eval $(ps -ef | grep "[0-9] python server\\.py a" | awk '{print "kill "$2}') -nohup python server.py a >> ssserver.log 2>&1 & +ulimit -n 4096 +nohup python server.py a >> /dev/null 2>&1 &