]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon: make idle timeout for incoming connection configurable
authorGrigorii Demidov <grigorii.demidov@nic.cz>
Tue, 26 Jun 2018 08:49:23 +0000 (10:49 +0200)
committerPetr Špaček <petr.spacek@nic.cz>
Mon, 16 Jul 2018 09:04:04 +0000 (09:04 +0000)
daemon/bindings.c
daemon/io.c
daemon/network.c
daemon/network.h

index fe489331e3f2cf899df4098a77e29dce84d8ed88..18035e2b11fd8ed0af7e417df5368b7755265884 100644 (file)
@@ -793,6 +793,37 @@ static int net_outgoing(lua_State *L, int family)
 static int net_outgoing_v4(lua_State *L) { return net_outgoing(L, AF_INET); }
 static int net_outgoing_v6(lua_State *L) { return net_outgoing(L, AF_INET6); }
 
+static int net_tcp_in_idle(lua_State *L)
+{
+       struct engine *engine = engine_luaget(L);
+       struct network *net = &engine->net;
+
+       /* Only return current idle timeout. */
+       if (lua_gettop(L) == 0) {
+               lua_pushnumber(L, net->tcp.in_idle_timeout);
+               return 1;
+       }
+
+       if ((lua_gettop(L) != 1)) {
+               lua_pushstring(L, "net.tcp_in_idle takes one parameter: (\"idle timeout\")");
+               lua_error(L);
+       }
+
+       if (lua_isnumber(L, 1)) {
+               int idle_timeout = lua_tointeger(L, 1);
+               if (idle_timeout <= 0) {
+                       lua_pushstring(L, "net.tcp_in_idle parameter has to be positive number");
+                       lua_error(L);
+               }
+               net->tcp.in_idle_timeout = idle_timeout;
+       } else {
+               lua_pushstring(L, "net.tcp_in_idle parameter has to be positive number");
+               lua_error(L);
+       }
+       lua_pushboolean(L, true);
+       return 1;
+}
+
 int lib_net(lua_State *L)
 {
        static const luaL_Reg lib[] = {
@@ -810,6 +841,7 @@ int lib_net(lua_State *L)
                { "tls_sticket_secret_file", net_tls_sticket_secret_file },
                { "outgoing_v4",  net_outgoing_v4 },
                { "outgoing_v6",  net_outgoing_v6 },
+               { "tcp_in_idle",  net_tcp_in_idle },
                { NULL, NULL }
        };
        register_lib(L, "net", lib);
index faa854c815b21a3e8b6bc8351ef4f63081da6e11..b59e3d879240557331fd77393dc905ea8eeb239a 100644 (file)
@@ -301,6 +301,11 @@ static void _tcp_accept(uv_stream_t *master, int status, bool tls)
                return;
        }
 
+       const struct worker_ctx *worker = (struct worker_ctx *)master->loop->data;
+       const struct engine *engine = worker->engine;
+       const struct network *net = &engine->net;
+       uint64_t idle_in_timeout = net->tcp.in_idle_timeout;
+
        uint64_t timeout = KR_CONN_RTT_MAX / 2;
        session->has_tls = tls;
        if (tls) {
@@ -316,7 +321,7 @@ static void _tcp_accept(uv_stream_t *master, int status, bool tls)
                }
        }
        uv_timer_t *timer = &session->timeout;
-       uv_timer_start(timer, tcp_timeout_trigger, timeout, timeout);
+       uv_timer_start(timer, tcp_timeout_trigger, timeout, idle_in_timeout);
        io_start_read((uv_handle_t *)client);
 }
 
index 77186d64c335ddee8eead7a070ffbdbcf0c94e4d..a790474deb28f7b3a71bc641b1f5b38e4cc3bc16 100644 (file)
@@ -54,6 +54,7 @@ void network_init(struct network *net, uv_loop_t *loop)
                net->tls_client_params = map_make(NULL);
                net->tls_session_ticket_ctx = /* unsync. random, by default */
                        tls_session_ticket_ctx_create(loop, NULL, 0);
+               net->tcp.in_idle_timeout = 10000;
        }
 }
 
@@ -112,6 +113,7 @@ void network_deinit(struct network *net)
                tls_client_params_free(&net->tls_client_params);
                net->tls_credentials = NULL;
                tls_session_ticket_ctx_destroy(net->tls_session_ticket_ctx);
+               net->tcp.in_idle_timeout = 0;
        }
 }
 
index 241e595d762a153ec05084c252990592724fea0e..15aac9e0ba1cb209d110178c7f5cd048d749ab63 100644 (file)
@@ -42,6 +42,10 @@ struct endpoint {
 typedef array_t(struct endpoint*) endpoint_array_t;
 /* @endcond */
 
+struct net_tcp_param {
+       uint64_t in_idle_timeout;
+};
+
 struct tls_session_ticket_ctx;
 struct network {
        uv_loop_t *loop;
@@ -49,6 +53,7 @@ struct network {
        struct tls_credentials *tls_credentials;
        map_t tls_client_params;
        struct tls_session_ticket_ctx *tls_session_ticket_ctx;
+       struct net_tcp_param tcp;
 };
 
 void network_init(struct network *net, uv_loop_t *loop);