diff --git a/src/jconf.h b/src/jconf.h index ff324868..59408c54 100644 --- a/src/jconf.h +++ b/src/jconf.h @@ -25,9 +25,10 @@ #define MAX_PORT_NUM 1024 #define MAX_REMOTE_NUM 10 #define MAX_DSCP_NUM 64 -#define MAX_CONF_SIZE 128 * 1024 +#define MAX_CONF_SIZE (128 * 1024) #define MAX_CONNECT_TIMEOUT 10 #define MAX_REQUEST_TIMEOUT 30 +#define MIN_TCP_IDLE_TIMEOUT (24 * 3600) #define MIN_UDP_TIMEOUT 10 #define DSCP_EF 0x2E diff --git a/src/server.c b/src/server.c index c076e0cc..341f1b62 100644 --- a/src/server.c +++ b/src/server.c @@ -704,6 +704,9 @@ server_recv_cb(EV_P_ ev_io *w, int revents) if (server->stage == STAGE_STREAM) { remote = server->remote; buf = remote->buf; + + // Only timer the watcher if a valid connection is established + ev_timer_again(EV_A_ & server->recv_ctx->watcher); } ssize_t r = recv(server->fd, buf->data, SOCKET_BUF_SIZE, 0); @@ -1107,6 +1110,8 @@ remote_recv_cb(EV_P_ ev_io *w, int revents) return; } + ev_timer_again(EV_A_ & server->recv_ctx->watcher); + ssize_t r = recv(remote->fd, server->buf->data, SOCKET_BUF_SIZE, 0); if (r == 0) { @@ -1229,9 +1234,6 @@ remote_send_cb(EV_P_ ev_io *w, int revents) int r = getpeername(remote->fd, (struct sockaddr *)&addr, &len); if (r == 0) { - // connection connected, stop the request timeout timer - ev_timer_stop(EV_A_ & server->recv_ctx->watcher); - remote_send_ctx->connected = 1; if (remote->buf->len == 0) { @@ -1391,10 +1393,13 @@ new_server(int fd, listen_ctx_t *listener) int request_timeout = min(MAX_REQUEST_TIMEOUT, listener->timeout) + rand() % MAX_REQUEST_TIMEOUT; + int repeat_interval = max(MIN_TCP_IDLE_TIMEOUT, listener->timeout) + + rand() % listener->timeout; + ev_io_init(&server->recv_ctx->io, server_recv_cb, fd, EV_READ); ev_io_init(&server->send_ctx->io, server_send_cb, fd, EV_WRITE); ev_timer_init(&server->recv_ctx->watcher, server_timeout_cb, - request_timeout, 0); + request_timeout, repeat_interval); cork_dllist_add(&connections, &server->entries);