Browse Source

better way to stop a server

dev
BreakWa11 9 years ago
parent
commit
4e10ff9063
  1. 2
      Config.py
  2. 12
      db_transfer.py
  3. 8
      server_pool.py
  4. 5
      shadowsocks/tcprelay.py
  5. 22
      shadowsocks/udprelay.py

2
Config.py

@ -5,7 +5,7 @@ MYSQL_USER = 'ss'
MYSQL_PASS = 'ss' MYSQL_PASS = 'ss'
MYSQL_DB = 'shadowsocks' MYSQL_DB = 'shadowsocks'
MYSQL_TRANSFER_MUL = 1.0 MYSQL_TRANSFER_MUL = 1.0
MYSQL_UPDATE_TIME = 15 MYSQL_UPDATE_TIME = 60
MANAGE_PASS = 'ss233333333' MANAGE_PASS = 'ss233333333'
#if you want manage in other server you should set this value to global ip #if you want manage in other server you should set this value to global ip

12
db_transfer.py

@ -115,6 +115,7 @@ class DbTransfer(object):
except Exception as e: except Exception as e:
logging.error('load switchrule.py fail') logging.error('load switchrule.py fail')
cur_servers = {} cur_servers = {}
new_servers = {}
for row in rows: for row in rows:
try: try:
allow = switchrule.isTurnOn(row) and row['enable'] == 1 and row['u'] + row['d'] < row['transfer_enable'] allow = switchrule.isTurnOn(row) and row['enable'] == 1 and row['u'] + row['d'] < row['transfer_enable']
@ -139,9 +140,10 @@ class DbTransfer(object):
#password changed #password changed
logging.info('db stop server at port [%s] reason: password changed' % (port,)) logging.info('db stop server at port [%s] reason: password changed' % (port,))
ServerPool.get_instance().cb_del_server(port) ServerPool.get_instance().cb_del_server(port)
ServerPool.get_instance().new_server(port, passwd) new_servers[port] = passwd
elif allow and ServerPool.get_instance().server_run_status(port) is False: elif allow and ServerPool.get_instance().server_run_status(port) is False:
#new_servers[port] = passwd
logging.info('db start server at port [%s] pass [%s]' % (port, passwd)) logging.info('db start server at port [%s] pass [%s]' % (port, passwd))
ServerPool.get_instance().new_server(port, passwd) ServerPool.get_instance().new_server(port, passwd)
@ -152,6 +154,14 @@ class DbTransfer(object):
logging.info('db stop server at port [%s] reason: port not exist' % (row['port'])) logging.info('db stop server at port [%s] reason: port not exist' % (row['port']))
ServerPool.get_instance().cb_del_server(row['port']) ServerPool.get_instance().cb_del_server(row['port'])
if len(new_servers) > 0:
from shadowsocks import eventloop
DbTransfer.get_instance().event.wait(eventloop.TIMEOUT_PRECISION)
for port in new_servers.keys():
passwd = new_servers[port]
logging.info('db start server at port [%s] pass [%s]' % (port, passwd))
ServerPool.get_instance().new_server(port, passwd)
@staticmethod @staticmethod
def del_servers(): def del_servers():
for port in ServerPool.get_instance().tcp_servers_pool.keys(): for port in ServerPool.get_instance().tcp_servers_pool.keys():

8
server_pool.py

@ -186,12 +186,12 @@ class ServerPool(object):
else: else:
logging.info("stopped server at %s:%d" % (self.config['server'], port)) logging.info("stopped server at %s:%d" % (self.config['server'], port))
try: try:
self.tcp_servers_pool[port].close(False) self.tcp_servers_pool[port].close(True)
del self.tcp_servers_pool[port] del self.tcp_servers_pool[port]
except Exception as e: except Exception as e:
logging.warn(e) logging.warn(e)
try: try:
self.udp_servers_pool[port].close(False) self.udp_servers_pool[port].close(True)
del self.udp_servers_pool[port] del self.udp_servers_pool[port]
except Exception as e: except Exception as e:
logging.warn(e) logging.warn(e)
@ -202,12 +202,12 @@ class ServerPool(object):
else: else:
logging.info("stopped server at [%s]:%d" % (self.config['server_ipv6'], port)) logging.info("stopped server at [%s]:%d" % (self.config['server_ipv6'], port))
try: try:
self.tcp_ipv6_servers_pool[port].close(False) self.tcp_ipv6_servers_pool[port].close(True)
del self.tcp_ipv6_servers_pool[port] del self.tcp_ipv6_servers_pool[port]
except Exception as e: except Exception as e:
logging.warn(e) logging.warn(e)
try: try:
self.udp_ipv6_servers_pool[port].close(False) self.udp_ipv6_servers_pool[port].close(True)
del self.udp_ipv6_servers_pool[port] del self.udp_ipv6_servers_pool[port]
except Exception as e: except Exception as e:
logging.warn(e) logging.warn(e)

5
shadowsocks/tcprelay.py

@ -1018,9 +1018,8 @@ class TCPRelay(object):
self._server_socket.close() self._server_socket.close()
self._server_socket = None self._server_socket = None
logging.info('closed TCP port %d', self._listen_port) logging.info('closed TCP port %d', self._listen_port)
if not self._fd_to_handlers: for handler in list(self._fd_to_handlers.values()):
logging.info('stopping') handler.destroy()
self._eventloop.stop()
self._sweep_timeout() self._sweep_timeout()
def close(self, next_tick=False): def close(self, next_tick=False):

22
shadowsocks/udprelay.py

@ -1343,25 +1343,29 @@ class UDPRelay(object):
def handle_periodic(self): def handle_periodic(self):
if self._closed: if self._closed:
self._cache.clear(0)
self._dns_cache.sweep()
if self._eventloop:
self._eventloop.remove_periodic(self.handle_periodic)
self._eventloop.remove(self._server_socket)
if self._server_socket: if self._server_socket:
self._server_socket.close() self._server_socket.close()
self._server_socket = None self._server_socket = None
#for sock in self._sockets:
# sock.close()
logging.info('closed UDP port %d', self._listen_port) logging.info('closed UDP port %d', self._listen_port)
before_sweep_size = len(self._sockets) else:
self._cache.sweep() before_sweep_size = len(self._sockets)
self._dns_cache.sweep() self._cache.sweep()
if before_sweep_size != len(self._sockets): self._dns_cache.sweep()
logging.debug('UDP port %5d sockets %d' % (self._listen_port, len(self._sockets))) if before_sweep_size != len(self._sockets):
self._sweep_timeout() logging.debug('UDP port %5d sockets %d' % (self._listen_port, len(self._sockets)))
self._sweep_timeout()
def close(self, next_tick=False): def close(self, next_tick=False):
logging.debug('UDP close') logging.debug('UDP close')
self._closed = True self._closed = True
if not next_tick: if not next_tick:
self._cache.clear(0)
if self._eventloop: if self._eventloop:
self._eventloop.remove_periodic(self.handle_periodic) self._eventloop.remove_periodic(self.handle_periodic)
self._eventloop.remove(self._server_socket) self._eventloop.remove(self._server_socket)
self._server_socket.close() self._server_socket.close()
self._cache.clear(0)

Loading…
Cancel
Save