From c8627c0d1b866b567b52a1540b313d6e4e448842 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Thu, 30 Oct 2014 14:58:59 +1100 Subject: [PATCH] Listen with the maximum backlog when we first start for more seamless restarts and then drop to the minimum afterwards to effectively ratelimit new connections --- src/connector.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/connector.c b/src/connector.c index 35289338..d79500e6 100644 --- a/src/connector.c +++ b/src/connector.c @@ -294,8 +294,8 @@ void *receiver(void *arg) conn_instance_t *ci = (conn_instance_t *)arg; client_instance_t *client, *tmp; int ret, nfds, i, maxfds = 1; + bool update, maxconn = true; struct pollfd *fds; - bool update; rename_proc("creceiver"); @@ -379,6 +379,14 @@ repoll: if (update) goto rebuild_fds; + else if (unlikely(maxconn)) { + /* When we first start we listen to as many connections as + * possible. Once we stop receiving connections we drop the + * listen to the minimum to effectively ratelimit how fast we + * can receive connections. */ + maxconn = false; + listen(ci->serverfd, 0); + } goto repoll; out: free(fds); @@ -748,7 +756,7 @@ int connector(proc_instance_t *pi) if (tries) LOGWARNING("Connector successfully bound to socket"); - ret = listen(sockd, 10); + ret = listen(sockd, SOMAXCONN); if (ret < 0) { LOGERR("Connector failed to listen on socket"); Close(sockd);