src/server/daemon/sessionhandler.c

changeset 543
3335f431a91b
parent 542
1327febf99c4
child 544
27684460629f
--- a/src/server/daemon/sessionhandler.c	Sun Aug 11 18:51:39 2024 +0200
+++ b/src/server/daemon/sessionhandler.c	Mon Aug 12 00:22:37 2024 +0200
@@ -497,34 +497,27 @@
         return;
     }
     
-    EVWatchList *keepalive = malloc(sizeof(EVWatchList));
-    if(!keepalive) {
-        free(event);
-        connection_destroy(conn);
-        return;
-    }
-    
-    ZERO(keepalive, sizeof(EVWatchList));
-    keepalive->destroy = evt_keep_alive_destroy;
-    keepalive->data = conn; 
-    
     ZERO(event, sizeof(Event));
     event->fn = evt_keep_alive_enqueue;
     event->finish = ev_free_event; // this will free the event obj at the end
-    event->cookie = keepalive;
+    event->cookie = conn;
     
     EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler);
     if(event_send(ev, event)) {
         log_ereport(LOG_FAILURE, "Keep-Alive: ev_send failed");
         connection_destroy(conn);
         free(event);
-        free(keepalive);
     }
 }
 
-int evt_keep_alive_enqueue(EventHandler *h, Event *event) {
-    EVWatchList *keepalive = event->cookie;
-    Connection *conn = keepalive->data;
+int evt_keep_alive_enqueue(EventHandler *h, Event *event) { 
+    Connection *conn = event->cookie;
+    
+    EVWatchList *keepalive = malloc(sizeof(EVWatchList));
+    if(!keepalive) {
+        connection_destroy(conn);
+        return 0;
+    }
     
     Event *ioevent = malloc(sizeof(Event));
     if(!ioevent) {
@@ -535,8 +528,12 @@
     
     // add keepalive object to the eventhandler watchlist
     // the watchlist will check the timeout
+    ZERO(keepalive, sizeof(EVWatchList));
+    keepalive->data1 = conn;
+    keepalive->data2 = ioevent;
+    keepalive->destroy = evt_keep_alive_destroy;
     keepalive->created = time(NULL);
-    keepalive->expire = keepalive->created + 60; // TODO: config
+    keepalive->expire = keepalive->created + 10; // TODO: config
     ev_watchlist_add(h, keepalive);
     
     // wait for input
@@ -557,7 +554,7 @@
 
 int evt_keep_alive_input_event(EventHandler *h, Event *event) {
     EVWatchList *keepalive = event->cookie;
-    Connection *conn = keepalive->data;
+    Connection *conn = keepalive->data1;
     
     // remove connection from the keep-alive list
     ev_watchlist_remove(h, keepalive);
@@ -579,5 +576,15 @@
 }
 
 void evt_keep_alive_destroy(EventHandler *h, EVWatchList *item) {
+    Connection *conn = item->data1;
+    Event *ioevent = item->data2;
+    log_ereport(LOG_DEBUG, "sessionhandler: keep-alive timeout: close connection");
     
+    if(ev_remove_poll(h, conn->fd)) {
+        log_ereport(LOG_FAILURE, "sessionhandler: keep-alive timeout: cannot remove poll");
+    }
+    
+    connection_destroy(conn);
+    free(ioevent);
+    free(item);
 }

mercurial