Skip to content

Commit df7614f

Browse files
author
clowwindy
committed
support workers
1 parent eb8fcca commit df7614f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

shadowsocks/server.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import logging
4848
import getopt
4949
import encrypt
50+
import os
5051
import utils
5152
import udprelay
5253

@@ -176,7 +177,7 @@ def main():
176177
config_path = utils.find_config()
177178
try:
178179
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:',
179-
['fast-open'])
180+
['fast-open', 'workers:'])
180181
for key, value in optlist:
181182
if key == '-c':
182183
config_path = value
@@ -194,7 +195,7 @@ def main():
194195
config = {}
195196

196197
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:',
197-
['fast-open'])
198+
['fast-open', 'workers='])
198199
for key, value in optlist:
199200
if key == '-p':
200201
config['server_port'] = int(value)
@@ -206,6 +207,8 @@ def main():
206207
config['method'] = value
207208
elif key == '--fast-open':
208209
config['fast_open'] = True
210+
elif key == '--workers':
211+
config['workers'] = value
209212
except getopt.GetoptError:
210213
utils.print_server_help()
211214
sys.exit(2)
@@ -217,6 +220,7 @@ def main():
217220
config_port_password = config.get('port_password', None)
218221
config_timeout = config.get('timeout', 600)
219222
config_fast_open = config.get('fast_open', False)
223+
config_workers = config.get('workers', 1)
220224

221225
if not config_key and not config_path:
222226
sys.exit('config not specified, please read '
@@ -249,6 +253,17 @@ def main():
249253
udprelay.UDPRelay(config_server, int(port), None, None, key,
250254
config_method, int(config_timeout), False).start()
251255

256+
if int(config_workers) > 1:
257+
if os.name == 'posix':
258+
# TODO only serve in workers, not in master
259+
for i in xrange(0, int(config_workers) - 1):
260+
r = os.fork()
261+
if r == 0:
262+
break
263+
else:
264+
logging.warn('worker is only available on Unix/Linux')
265+
266+
252267

253268
if __name__ == '__main__':
254269
try:

shadowsocks/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,5 @@ def print_server_help():
125125
-m METHOD encryption method, for example, aes-256-cfb
126126
-c CONFIG path to config file
127127
--fast-open use TCP_FASTOPEN, requires Linux 3.7+
128+
--workers WORKERS number of workers, available on Unix/Linux
128129
'''

0 commit comments

Comments
 (0)