Merge 82cb31f3b6 into 17175afd73
This commit is contained in:
commit
c69646900b
21
websocket.py
21
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
|
||||||
|
|
@ -831,12 +832,17 @@ Sec-WebSocket-Accept: %s\r
|
||||||
# os.fork() (python 2.4) child reaper
|
# os.fork() (python 2.4) child reaper
|
||||||
signal.signal(signal.SIGCHLD, self.fallback_SIGCHLD)
|
signal.signal(signal.SIGCHLD, self.fallback_SIGCHLD)
|
||||||
|
|
||||||
|
last_active_time = self.launch_time
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
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 +850,19 @@ Sec-WebSocket-Accept: %s\r
|
||||||
% self.timeout)
|
% self.timeout)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if self.idle_timeout:
|
||||||
|
idle_time = 0
|
||||||
|
if child_count == 0:
|
||||||
|
idle_time = time.time() - last_active_time
|
||||||
|
else:
|
||||||
|
idle_time = 0
|
||||||
|
last_active_time = time.time()
|
||||||
|
|
||||||
|
if idle_time > 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