Skip to content

Commit

Permalink
hot load config
Browse files Browse the repository at this point in the history
support userapiconfig
  • Loading branch information
breakwa11 committed Jun 8, 2016
1 parent 4d43e12 commit 569aaa5
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Config.py → apiconfig.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,11 +12,10 @@
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)
Expand Down
1 change: 0 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@
"connect_verbose_info": 0,
"redirect": "",
"fast_open": false,
"workers": 1
}
27 changes: 27 additions & 0 deletions configloader.py
Original file line number Diff line number Diff line change
@@ -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()

26 changes: 13 additions & 13 deletions db_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
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):

Expand Down Expand Up @@ -39,16 +39,16 @@ def push_db_all_user(self):
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 = ''
Expand All @@ -71,8 +71,8 @@ def push_db_all_user(self):
' 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()
Expand All @@ -90,8 +90,8 @@ def pull_db_all_user():
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 = []
Expand Down Expand Up @@ -180,7 +180,7 @@ def thread_db():
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()
Expand All @@ -190,7 +190,7 @@ def thread_db():
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
Expand Down
6 changes: 6 additions & 0 deletions logrun.sh
Original file line number Diff line number Diff line change
@@ -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 &

2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 &

13 changes: 5 additions & 8 deletions server_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -127,6 +122,7 @@ def new_server(self, port, password):
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))

Expand All @@ -151,6 +147,7 @@ def new_server(self, port, password):
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))

Expand All @@ -173,7 +170,7 @@ def del_server(self, port):
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)
Expand Down
6 changes: 6 additions & 0 deletions shadowsocks/logrun.sh
Original file line number Diff line number Diff line change
@@ -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 &

3 changes: 2 additions & 1 deletion shadowsocks/run.sh
Original file line number Diff line number Diff line change
@@ -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 &

0 comments on commit 569aaa5

Please sign in to comment.