Browse Source

support workers

1.4
clowwindy 11 years ago
parent
commit
df7614f26c
  1. 19
      shadowsocks/server.py
  2. 1
      shadowsocks/utils.py

19
shadowsocks/server.py

@ -47,6 +47,7 @@ import struct
import logging import logging
import getopt import getopt
import encrypt import encrypt
import os
import utils import utils
import udprelay import udprelay
@ -176,7 +177,7 @@ def main():
config_path = utils.find_config() config_path = utils.find_config()
try: try:
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:', optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:',
['fast-open']) ['fast-open', 'workers:'])
for key, value in optlist: for key, value in optlist:
if key == '-c': if key == '-c':
config_path = value config_path = value
@ -194,7 +195,7 @@ def main():
config = {} config = {}
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:', optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:',
['fast-open']) ['fast-open', 'workers='])
for key, value in optlist: for key, value in optlist:
if key == '-p': if key == '-p':
config['server_port'] = int(value) config['server_port'] = int(value)
@ -206,6 +207,8 @@ def main():
config['method'] = value config['method'] = value
elif key == '--fast-open': elif key == '--fast-open':
config['fast_open'] = True config['fast_open'] = True
elif key == '--workers':
config['workers'] = value
except getopt.GetoptError: except getopt.GetoptError:
utils.print_server_help() utils.print_server_help()
sys.exit(2) sys.exit(2)
@ -217,6 +220,7 @@ def main():
config_port_password = config.get('port_password', None) config_port_password = config.get('port_password', None)
config_timeout = config.get('timeout', 600) config_timeout = config.get('timeout', 600)
config_fast_open = config.get('fast_open', False) config_fast_open = config.get('fast_open', False)
config_workers = config.get('workers', 1)
if not config_key and not config_path: if not config_key and not config_path:
sys.exit('config not specified, please read ' sys.exit('config not specified, please read '
@ -249,6 +253,17 @@ def main():
udprelay.UDPRelay(config_server, int(port), None, None, key, udprelay.UDPRelay(config_server, int(port), None, None, key,
config_method, int(config_timeout), False).start() config_method, int(config_timeout), False).start()
if int(config_workers) > 1:
if os.name == 'posix':
# TODO only serve in workers, not in master
for i in xrange(0, int(config_workers) - 1):
r = os.fork()
if r == 0:
break
else:
logging.warn('worker is only available on Unix/Linux')
if __name__ == '__main__': if __name__ == '__main__':
try: try:

1
shadowsocks/utils.py

@ -125,4 +125,5 @@ optional arguments:
-m METHOD encryption method, for example, aes-256-cfb -m METHOD encryption method, for example, aes-256-cfb
-c CONFIG path to config file -c CONFIG path to config file
--fast-open use TCP_FASTOPEN, requires Linux 3.7+ --fast-open use TCP_FASTOPEN, requires Linux 3.7+
--workers WORKERS number of workers, available on Unix/Linux
''' '''
Loading…
Cancel
Save