From 37ae4386bd764d62ae43c68df1560be55ff813ca Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 19 Oct 2015 11:51:51 +0200 Subject: [PATCH] Prioritize HTTP sockets over outgoing connections --- src/connection.c | 13 ++++++++----- src/eventl.c | 8 ++++---- src/eventl.h | 2 +- src/http.c | 8 +++++--- src/http_command.c | 2 +- src/test_sel_thread.c | 4 ++-- 6 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/connection.c b/src/connection.c index fbdaf79..80d6ec0 100644 --- a/src/connection.c +++ b/src/connection.c @@ -336,8 +336,10 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man) 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); + 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); @@ -402,7 +404,8 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man) 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; } @@ -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); - 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); @@ -435,7 +438,7 @@ static int connection_connect(struct connection *con, iochan_man_t iochan_man) } else { - client_set_state(con->client, Client_Connecting); + client_set_state(cl, Client_Connecting); } ZOOM_options_destroy(zoptions); wrbuf_destroy(w); diff --git a/src/eventl.c b/src/eventl.c index 89138c2..ba4ba59 100644 --- a/src/eventl.c +++ b/src/eventl.c @@ -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; - 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; } @@ -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; @@ -181,7 +181,7 @@ int iochan_add(iochan_man_t man, IOCHAN chan) 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); diff --git a/src/eventl.h b/src/eventl.h index 7fad3d3..515b7f2 100644 --- a/src/eventl.h +++ b/src/eventl.h @@ -52,7 +52,7 @@ typedef struct iochan 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); diff --git a/src/http.c b/src/http.c index a4f0844..5f5534d 100644 --- a/src/http.c +++ b/src/http.c @@ -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); - if (iochan_add(ser->iochan_man, p->iochan)) + if (iochan_add(ser->iochan_man, p->iochan, 5)) { 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); - 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); } } @@ -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"); - 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; } diff --git a/src/http_command.c b/src/http_command.c index 1e3ede6..dcb21a7 100644 --- a/src/http_command.c +++ b/src/http_command.c @@ -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); - 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; } diff --git a/src/test_sel_thread.c b/src/test_sel_thread.c index 0284da1..25447d8 100644 --- a/src/test_sel_thread.c +++ b/src/test_sel_thread.c @@ -107,12 +107,12 @@ static void test_for_real_work(int no_threads) 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_add(chan_man, chan); + iochan_add(chan_man, chan, -1); iochan_man_events(chan_man); sel_thread_destroy(p); -- 1.7.10.4