From df7614f26cca3a744f52b15fcef82af6d76376f7 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sat, 17 May 2014 12:44:12 +0800 Subject: [PATCH] support workers --- shadowsocks/server.py | 19 +++++++++++++++++-- shadowsocks/utils.py | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/shadowsocks/server.py b/shadowsocks/server.py index 8493b0a..bb31bf2 100755 --- a/shadowsocks/server.py +++ b/shadowsocks/server.py @@ -47,6 +47,7 @@ import struct import logging import getopt import encrypt +import os import utils import udprelay @@ -176,7 +177,7 @@ def main(): config_path = utils.find_config() try: optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:', - ['fast-open']) + ['fast-open', 'workers:']) for key, value in optlist: if key == '-c': config_path = value @@ -194,7 +195,7 @@ def main(): config = {} optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:', - ['fast-open']) + ['fast-open', 'workers=']) for key, value in optlist: if key == '-p': config['server_port'] = int(value) @@ -206,6 +207,8 @@ def main(): config['method'] = value elif key == '--fast-open': config['fast_open'] = True + elif key == '--workers': + config['workers'] = value except getopt.GetoptError: utils.print_server_help() sys.exit(2) @@ -217,6 +220,7 @@ def main(): config_port_password = config.get('port_password', None) config_timeout = config.get('timeout', 600) config_fast_open = config.get('fast_open', False) + config_workers = config.get('workers', 1) if not config_key and not config_path: sys.exit('config not specified, please read ' @@ -249,6 +253,17 @@ def main(): udprelay.UDPRelay(config_server, int(port), None, None, key, 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__': try: diff --git a/shadowsocks/utils.py b/shadowsocks/utils.py index f5bcba3..eba8200 100644 --- a/shadowsocks/utils.py +++ b/shadowsocks/utils.py @@ -125,4 +125,5 @@ optional arguments: -m METHOD encryption method, for example, aes-256-cfb -c CONFIG path to config file --fast-open use TCP_FASTOPEN, requires Linux 3.7+ + --workers WORKERS number of workers, available on Unix/Linux ''' \ No newline at end of file