diff --git a/core/uwsgi.c b/core/uwsgi.c index a26e44f4..931cfb12 100644 --- a/core/uwsgi.c +++ b/core/uwsgi.c @@ -495,6 +495,9 @@ static struct uwsgi_option uwsgi_base_options[] = { {"websockets-ping-freq", required_argument, 0, "set the frequency (in seconds) of websockets automatic ping packets", uwsgi_opt_set_int, &uwsgi.websockets_ping_freq, 0}, {"websocket-ping-freq", required_argument, 0, "set the frequency (in seconds) of websockets automatic ping packets", uwsgi_opt_set_int, &uwsgi.websockets_ping_freq, 0}, + {"websockets-pong-tolerance", required_argument, 0, "set the tolerance (in seconds) of websockets ping/pong subsystem", uwsgi_opt_set_int, &uwsgi.websockets_pong_tolerance, 0}, + {"websocket-pong-tolerance", required_argument, 0, "set the tolerance (in seconds) of websockets ping/pong subsystem", uwsgi_opt_set_int, &uwsgi.websockets_pong_tolerance, 0}, + {"websockets-max-size", required_argument, 0, "set the max allowed size of websocket messages (in Kbytes, default 1024)", uwsgi_opt_set_64bit, &uwsgi.websockets_max_size, 0}, {"websocket-max-size", required_argument, 0, "set the max allowed size of websocket messages (in Kbytes, default 1024)", uwsgi_opt_set_64bit, &uwsgi.websockets_max_size, 0}, diff --git a/core/websockets.c b/core/websockets.c index 8048d323..cd2c9e76 100644 --- a/core/websockets.c +++ b/core/websockets.c @@ -47,19 +47,22 @@ static int uwsgi_websockets_pong(struct wsgi_request *wsgi_req) { static int uwsgi_websockets_check_pingpong(struct wsgi_request *wsgi_req) { time_t now = uwsgi_now(); - if (wsgi_req->websocket_last_ping == 0 || - (now - wsgi_req->websocket_last_ping > uwsgi.websockets_ping_freq)) { - if (uwsgi_websockets_ping(wsgi_req)) return -1; - } - else if (wsgi_req->websocket_last_ping > 0) { - if (wsgi_req->websocket_last_pong < wsgi_req->websocket_last_ping) { - if (wsgi_req->websocket_last_ping - wsgi_req->websocket_last_pong > - uwsgi.websockets_ping_freq) { - uwsgi_log("[uwsgi-websocket] no PONG received in %d seconds !!!\n", uwsgi.websockets_ping_freq); - return -1; - } - } - } + // first round + if (wsgi_req->websocket_last_ping == 0) { + return uwsgi_websockets_ping(wsgi_req); + } + // pong not received ? + if (wsgi_req->websocket_last_pong < wsgi_req->websocket_last_ping) { + if (wsgi_req->websocket_last_ping - wsgi_req->websocket_last_pong > uwsgi.websockets_pong_tolerance) { + uwsgi_log("[uwsgi-websocket] no PONG received in %d seconds !!!\n", uwsgi.websockets_pong_tolerance); + return -1; + } + return 0; + } + // pong received, send anther ping + if (now - wsgi_req->websocket_last_ping >= uwsgi.websockets_ping_freq) { + return uwsgi_websockets_ping(wsgi_req); + } return 0; } @@ -351,5 +354,6 @@ void uwsgi_websockets_init() { uwsgi.websockets_ping = uwsgi_buffer_new(2); uwsgi_buffer_append(uwsgi.websockets_ping, "\x89\0", 2); uwsgi.websockets_ping_freq = 30; + uwsgi.websockets_pong_tolerance = 3; uwsgi.websockets_max_size = 1024; } diff --git a/plugins/corerouter/corerouter.c b/plugins/corerouter/corerouter.c index 1c7bfe81..e89fedfd 100644 --- a/plugins/corerouter/corerouter.c +++ b/plugins/corerouter/corerouter.c @@ -603,7 +603,7 @@ void uwsgi_corerouter_loop(int id, void *data) { if (!ucr->socket_timeout) - ucr->socket_timeout = 30; + ucr->socket_timeout = 60; if (!ucr->static_node_gracetime) ucr->static_node_gracetime = 30; diff --git a/uwsgi.h b/uwsgi.h index 149860a3..f56e0111 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -2187,6 +2187,7 @@ struct uwsgi_server { struct uwsgi_buffer *websockets_ping; struct uwsgi_buffer *websockets_pong; int websockets_ping_freq; + int websockets_pong_tolerance; uint64_t websockets_max_size; int (*wait_write_hook) (int, int);