]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
session_kill_ioreq(): worker.c -> session.c
authorVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 18 Sep 2018 12:26:26 +0000 (14:26 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Fri, 12 Oct 2018 15:36:43 +0000 (17:36 +0200)
daemon/session.c
daemon/session.h
daemon/worker.c

index 1d84b831be291a7769e7050ed035f86956e95e21..73e6d0cf7b28818585709391c8518292910cd052 100644 (file)
@@ -691,3 +691,58 @@ int session_wirebuf_process(struct session *session)
        return ret;
 }
 
+static void on_session_idle_timeout(uv_timer_t *timer)
+{
+       struct session *s = timer->data;
+       assert(s);
+       uv_timer_stop(timer);
+       if (s->sflags.closing) {
+               return;
+       }
+       /* session was not in use during timer timeout
+        * remove it from connection list and close
+        */
+       assert(session_is_empty(s));
+       session_close(s);
+}
+
+void session_kill_ioreq(struct session *s, struct qr_task *task)
+{
+       assert(s && s->sflags.outgoing && s->handle);
+       if (s->sflags.closing) {
+               return;
+       }
+       if (s->handle->type == UV_UDP) {
+               uv_timer_stop(&s->timeout);
+               session_tasklist_del(s, task);
+               assert(session_tasklist_is_empty(s));
+               session_close(s);
+               return;
+       }
+       /* TCP-specific code now. */
+       if (s->handle->type != UV_TCP) abort();
+       session_waitinglist_del(s, task);
+       session_tasklist_del(s, task);
+
+       int res = 0;
+
+       const struct sockaddr *peer = &s->peer.ip;
+       if (peer->sa_family != AF_UNSPEC && session_is_empty(s) && !s->sflags.closing) {
+               assert(peer->sa_family == AF_INET || peer->sa_family == AF_INET6);
+               res = 1;
+               if (s->sflags.connected) {
+                       /* This is outbound TCP connection which can be reused.
+                       * Close it after timeout */
+                       s->timeout.data = s;
+                       uv_timer_stop(&s->timeout);
+                       res = uv_timer_start(&s->timeout, on_session_idle_timeout,
+                                            KR_CONN_RTT_MAX, 0);
+               }
+       }
+
+       if (res != 0) {
+               /* if any errors, close the session immediately */
+               session_close(s);
+       }
+}
+
index d33aaa4cfda244353f8f73f4e7008a765290e3f2..aef3332e2e28e5de24607e8be19f20a790359566 100644 (file)
@@ -144,3 +144,6 @@ void session_unpoison(struct session *session);
 
 knot_pkt_t *session_produce_packet(struct session *session, knot_mm_t *mm);
 int session_discard_packet(struct session *session, const knot_pkt_t *pkt);
+
+void session_kill_ioreq(struct session *s, struct qr_task *task);
+
index 561f78a76a922494d62bddba8b7084fcf41b97c0..318f3e5d7c87b5a375b7de10f50f5bbc5d0bf229 100644 (file)
@@ -107,7 +107,6 @@ static int worker_del_tcp_waiting(struct worker_ctx *worker,
                                  const struct sockaddr *addr);
 static struct session* worker_find_tcp_waiting(struct worker_ctx *worker,
                                               const struct sockaddr *addr);
-static void on_session_idle_timeout(uv_timer_t *timer);
 static void on_tcp_connect_timeout(uv_timer_t *timer);
 static void on_tcp_watchdog_timeout(uv_timer_t *timer);
 
@@ -251,66 +250,10 @@ static uv_handle_t *ioreq_spawn(struct qr_task *task, int socktype, sa_family_t
        return handle;
 }
 
-static void ioreq_kill_udp(uv_handle_t *req, struct qr_task *task)
-{
-       assert(req);
-       struct session *s = req->data;
-       assert(session_is_outgoing(s));
-       if (session_is_closing(s)) {
-               return;
-       }
-       uv_timer_t *t = session_get_timer(s);
-       uv_timer_stop(t);
-       session_tasklist_del(s, task);
-       assert(session_tasklist_is_empty(s));
-       session_close(s);
-}
-
-static void ioreq_kill_tcp(uv_handle_t *req, struct qr_task *task)
-{
-       assert(req);
-       struct session *s = req->data;
-       assert(session_is_outgoing(s));
-       if (session_is_closing(s)) {
-               return;
-       }
-
-       session_waitinglist_del(s, task);
-       session_tasklist_del(s, task);
-
-       int res = 0;
-
-       const struct sockaddr *peer = session_get_peer(s);
-       if (peer->sa_family != AF_UNSPEC && session_is_empty(s) && !session_is_closing(s)) {
-               assert(peer->sa_family == AF_INET || peer->sa_family == AF_INET6);
-               res = 1;
-               if (session_is_connected(s)) {
-                       /* This is outbound TCP connection which can be reused.
-                       * Close it after timeout */
-                       uv_timer_t *t = session_get_timer(s);
-                       t->data = s;
-                       uv_timer_stop(t);
-                       res = uv_timer_start(t, on_session_idle_timeout,
-                                            KR_CONN_RTT_MAX, 0);
-               }
-       }
-
-       if (res != 0) {
-               /* if any errors, close the session immediately */
-               session_close(s);
-       }
-}
-
 static void ioreq_kill_pending(struct qr_task *task)
 {
        for (uint16_t i = 0; i < task->pending_count; ++i) {
-               if (task->pending[i]->type == UV_UDP) {
-                       ioreq_kill_udp(task->pending[i], task);
-               } else if (task->pending[i]->type == UV_TCP) {
-                       ioreq_kill_tcp(task->pending[i], task);
-               } else {
-                       assert(false);
-               }
+               session_kill_ioreq(task->pending[i]->data, task);
        }
        task->pending_count = 0;
 }
@@ -1116,21 +1059,6 @@ static void on_udp_timeout(uv_timer_t *timer)
        qr_task_step(task, NULL, NULL);
 }
 
-static void on_session_idle_timeout(uv_timer_t *timer)
-{
-       struct session *s = timer->data;
-       assert(s);
-       uv_timer_stop(timer);
-       if (session_is_closing(s)) {
-               return;
-       }
-       /* session was not in use during timer timeout
-        * remove it from connection list and close
-        */
-       assert(session_is_empty(s));
-       session_close(s);
-}
-
 static uv_handle_t *retransmit(struct qr_task *task)
 {
        uv_handle_t *ret = NULL;