Prioritize HTTP sockets over outgoing connections
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 19 Oct 2015 09:51:51 +0000 (11:51 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 19 Oct 2015 09:51:51 +0000 (11:51 +0200)
src/connection.c
src/eventl.c
src/eventl.h
src/http.c
src/http_command.c
src/test_sel_thread.c

index fbdaf79..80d6ec0 100644 (file)
@@ -336,8 +336,10 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
     int r = 0;
     WRBUF w;
 
     int r = 0;
     WRBUF w;
 
-    struct session_database *sdb = client_get_database(con->client);
+    struct client *cl = con->client;
+    struct session_database *sdb = client_get_database(cl);
     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
     const char *apdulog = session_setting_oneval(sdb, PZ_APDULOG);
+    struct session *se = client_get_session(cl);
     const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
     const char *redis = session_setting_oneval(sdb, PZ_REDIS);
 
     const char *memcached = session_setting_oneval(sdb, PZ_MEMCACHED);
     const char *redis = session_setting_oneval(sdb, PZ_REDIS);
 
@@ -402,7 +404,8 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
 
     if (!(con->link = ZOOM_connection_create(zoptions)))
     {
 
     if (!(con->link = ZOOM_connection_create(zoptions)))
     {
-        yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to create ZOOM Connection");
+        session_log(se, YLOG_WARN, "%s: ZOOM_connection_create failed",
+                    client_get_id(cl));
         ZOOM_options_destroy(zoptions);
         return -1;
     }
         ZOOM_options_destroy(zoptions);
         return -1;
     }
@@ -424,9 +427,9 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
     con->state = Conn_Connecting;
     iochan_settimeout(con->iochan, con->operation_timeout);
     iochan_setdata(con->iochan, con);
     con->state = Conn_Connecting;
     iochan_settimeout(con->iochan, con->operation_timeout);
     iochan_setdata(con->iochan, con);
-    if (iochan_add(iochan_man, con->iochan))
+    if (iochan_add(iochan_man, con->iochan, 20))
     {
     {
-        yaz_log(YLOG_FATAL, "Out of connections");
+        session_log(se, YLOG_WARN, "%s: out of connections", client_get_id(cl));
         iochan_destroy(con->iochan);
         con->iochan = 0;
         ZOOM_connection_destroy(con->link);
         iochan_destroy(con->iochan);
         con->iochan = 0;
         ZOOM_connection_destroy(con->link);
@@ -435,7 +438,7 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man)
     }
     else
     {
     }
     else
     {
-        client_set_state(con->client, Client_Connecting);
+        client_set_state(cl, Client_Connecting);
     }
     ZOOM_options_destroy(zoptions);
     wrbuf_destroy(w);
     }
     ZOOM_options_destroy(zoptions);
     wrbuf_destroy(w);
index 89138c2..ba4ba59 100644 (file)
@@ -131,8 +131,8 @@ iochan_man_t iochan_man_create(int no_threads, int max_sockets)
 #endif
     if (max_sockets)
         man->limit_fd = max_sockets;
 #endif
     if (max_sockets)
         man->limit_fd = max_sockets;
-    yaz_log(YLOG_LOG, "iochan max threads %d max sockets %d",
-            no_threads, max_sockets);
+    yaz_log(YLOG_LOG, "iochan threads %d limit fd %d", no_threads,
+            man->limit_fd);
     yaz_mutex_create(&man->iochan_mutex);
     return man;
 }
     yaz_mutex_create(&man->iochan_mutex);
     return man;
 }
@@ -168,7 +168,7 @@ void iochan_man_destroy(iochan_man_t *mp)
     }
 }
 
     }
 }
 
-int iochan_add(iochan_man_t man, IOCHAN chan)
+int iochan_add(iochan_man_t man, IOCHAN chan, int slack)
 {
     int r = 0, no_fds = 0;
     IOCHAN p;
 {
     int r = 0, no_fds = 0;
     IOCHAN p;
@@ -181,7 +181,7 @@ int iochan_add(iochan_man_t man, IOCHAN chan)
         if (p->fd >= 0)
             no_fds++;
     }
         if (p->fd >= 0)
             no_fds++;
     }
-    if (chan->fd > 0 && man->limit_fd > 0 && no_fds >= man->limit_fd)
+    if (slack >= 0 && man->limit_fd > 0 && no_fds >= man->limit_fd - slack)
     {
         r = -1;
         yaz_log(YLOG_WARN, "max channels %d in use", no_fds);
     {
         r = -1;
         yaz_log(YLOG_WARN, "max channels %d in use", no_fds);
index 7fad3d3..515b7f2 100644 (file)
@@ -52,7 +52,7 @@ typedef struct iochan
 
 
 iochan_man_t iochan_man_create(int no_threads, int max_sockets);
 
 
 iochan_man_t iochan_man_create(int no_threads, int max_sockets);
-int iochan_add(iochan_man_t man, IOCHAN chan);
+int iochan_add(iochan_man_t man, IOCHAN chan, int slack);
 void iochan_man_events(iochan_man_t man);
 void iochan_man_destroy(iochan_man_t *mp);
 void iochan_destroy(IOCHAN chan);
 void iochan_man_events(iochan_man_t man);
 void iochan_man_destroy(iochan_man_t *mp);
 void iochan_destroy(IOCHAN chan);
index a4f0844..5f5534d 100644 (file)
@@ -799,7 +799,7 @@ static int http_proxy(struct http_request *rq)
         p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT, "http_proxy");
         iochan_setdata(p->iochan, p);
 
         p->iochan = iochan_create(sock, proxy_io, EVENT_INPUT, "http_proxy");
         iochan_setdata(p->iochan, p);
 
-        if (iochan_add(ser->iochan_man, p->iochan))
+        if (iochan_add(ser->iochan_man, p->iochan, 5))
         {
             iochan_destroy(p->iochan);
             xfree(p);
         {
             iochan_destroy(p->iochan);
             xfree(p);
@@ -1226,8 +1226,9 @@ static void http_accept(IOCHAN i, int event)
     ch = http_channel_create(server->http_server, host, server);
     ch->iochan = c;
     iochan_setdata(c, ch);
     ch = http_channel_create(server->http_server, host, server);
     ch->iochan = c;
     iochan_setdata(c, ch);
-    if (iochan_add(server->iochan_man, c))
+    if (iochan_add(server->iochan_man, c, 0))
     {
     {
+        yaz_log(YLOG_WARN, "Refusing incoming HTTP connection");
         http_channel_destroy(c);
     }
 }
         http_channel_destroy(c);
     }
 }
@@ -1339,8 +1340,9 @@ int http_init(struct conf_server *server, const char *record_fname)
     }
 
     c = iochan_create(s, http_accept, EVENT_INPUT|EVENT_EXCEPT, "http_server");
     }
 
     c = iochan_create(s, http_accept, EVENT_INPUT|EVENT_EXCEPT, "http_server");
-    if (iochan_add(server->iochan_man, c))
+    if (iochan_add(server->iochan_man, c, 0))
     {
     {
+        yaz_log(YLOG_WARN, "Can not create HTTP binding socket");
         iochan_destroy(c);
         return -1;
     }
         iochan_destroy(c);
         return -1;
     }
index 1e3ede6..dcb21a7 100644 (file)
@@ -195,7 +195,7 @@ struct http_session *http_session_create(struct conf_service *service,
                 r->timeout_iochan, service->session_timeout);
     iochan_settimeout(r->timeout_iochan, service->session_timeout);
 
                 r->timeout_iochan, service->session_timeout);
     iochan_settimeout(r->timeout_iochan, service->session_timeout);
 
-    iochan_add(service->server->iochan_man, r->timeout_iochan);
+    iochan_add(service->server->iochan_man, r->timeout_iochan, -1);
     http_session_use(1);
     return r;
 }
     http_session_use(1);
     return r;
 }
index 0284da1..25447d8 100644 (file)
@@ -107,12 +107,12 @@ static void test_for_real_work(int no_threads)
     YAZ_CHECK(p);
     if (p)
     {
     YAZ_CHECK(p);
     if (p)
     {
-        iochan_man_t chan_man = iochan_man_create(10);
+        iochan_man_t chan_man = iochan_man_create(10, 0);
         IOCHAN chan = iochan_create(thread_fd, iochan_handler,
                                     EVENT_INPUT|EVENT_TIMEOUT, "test_chan");
         iochan_settimeout(chan, 1);
         iochan_setdata(chan, p);
         IOCHAN chan = iochan_create(thread_fd, iochan_handler,
                                     EVENT_INPUT|EVENT_TIMEOUT, "test_chan");
         iochan_settimeout(chan, 1);
         iochan_setdata(chan, p);
-        iochan_add(chan_man, chan);
+        iochan_add(chan_man, chan, -1);
 
         iochan_man_events(chan_man);
         sel_thread_destroy(p);
 
         iochan_man_events(chan_man);
         sel_thread_destroy(p);