Implement option --idle-timeout
server exits after TIMEOUT seconds if there are no active connections
This commit is contained in:
parent
17175afd73
commit
9348dd5208
13
websocket.py
13
websocket.py
|
|
@ -101,7 +101,7 @@ Sec-WebSocket-Accept: %s\r
|
||||||
def __init__(self, listen_host='', listen_port=None, source_is_ipv6=False,
|
def __init__(self, listen_host='', listen_port=None, source_is_ipv6=False,
|
||||||
verbose=False, cert='', key='', ssl_only=None,
|
verbose=False, cert='', key='', ssl_only=None,
|
||||||
daemon=False, record='', web='',
|
daemon=False, record='', web='',
|
||||||
run_once=False, timeout=0):
|
run_once=False, timeout=0, idle_timeout=0):
|
||||||
|
|
||||||
# settings
|
# settings
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
|
|
@ -112,6 +112,7 @@ Sec-WebSocket-Accept: %s\r
|
||||||
self.daemon = daemon
|
self.daemon = daemon
|
||||||
self.run_once = run_once
|
self.run_once = run_once
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
self.idle_timeout = idle_timeout
|
||||||
|
|
||||||
self.launch_time = time.time()
|
self.launch_time = time.time()
|
||||||
self.ws_connection = False
|
self.ws_connection = False
|
||||||
|
|
@ -837,6 +838,10 @@ Sec-WebSocket-Accept: %s\r
|
||||||
self.client = None
|
self.client = None
|
||||||
startsock = None
|
startsock = None
|
||||||
pid = err = 0
|
pid = err = 0
|
||||||
|
child_count = 0
|
||||||
|
|
||||||
|
if multiprocessing and self.idle_timeout:
|
||||||
|
child_count = len(multiprocessing.active_children())
|
||||||
|
|
||||||
time_elapsed = time.time() - self.launch_time
|
time_elapsed = time.time() - self.launch_time
|
||||||
if self.timeout and time_elapsed > self.timeout:
|
if self.timeout and time_elapsed > self.timeout:
|
||||||
|
|
@ -844,6 +849,12 @@ Sec-WebSocket-Accept: %s\r
|
||||||
% self.timeout)
|
% self.timeout)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if self.idle_timeout and time_elapsed > self.idle_timeout \
|
||||||
|
and child_count == 0:
|
||||||
|
self.msg('listener exit due to --idle-timeout %s'
|
||||||
|
% self.idle_timeout)
|
||||||
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.poll()
|
self.poll()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -308,6 +308,9 @@ def websockify_init():
|
||||||
help="handle a single WebSocket connection and exit")
|
help="handle a single WebSocket connection and exit")
|
||||||
parser.add_option("--timeout", type=int, default=0,
|
parser.add_option("--timeout", type=int, default=0,
|
||||||
help="after TIMEOUT seconds exit when not connected")
|
help="after TIMEOUT seconds exit when not connected")
|
||||||
|
parser.add_option("--idle-timeout", type=int, default=0,
|
||||||
|
help="server exits after TIMEOUT seconds if there are no "
|
||||||
|
"active connections")
|
||||||
parser.add_option("--cert", default="self.pem",
|
parser.add_option("--cert", default="self.pem",
|
||||||
help="SSL certificate file")
|
help="SSL certificate file")
|
||||||
parser.add_option("--key", default=None,
|
parser.add_option("--key", default=None,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue