MEDIUM: checks: avoid accumulating TIME_WAITs during checks
Some checks which do not induce a close from the server accumulate
local TIME_WAIT sockets because they're cleanly shut down. Typically
TCP probes cause this. This is very problematic when there are many
servers, when the checks are fast or when local source ports are rare.
So now we'll disable lingering on the socket instead of sending a
shutdown. Before doing this we try to drain any possibly pending data.
That way we avoid sending an RST when the server has closed first.
This change means that some servers will see more RSTs, but this is
needed to avoid local source port starvation.
diff --git a/src/checks.c b/src/checks.c
index d38823b..da4cd95 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -1157,11 +1157,18 @@
*s->check.bi->data = '\0';
s->check.bi->i = 0;
- /* Close the connection... */
+ /* Close the connection... We absolutely want to perform a hard close
+ * and reset the connection if some data are pending, otherwise we end
+ * up with many TIME_WAITs and eat all the source port range quickly.
+ * To avoid sending RSTs all the time, we first try to drain pending
+ * data.
+ */
if (conn->xprt && conn->xprt->shutw)
conn->xprt->shutw(conn, 0);
- if (!(conn->flags & (CO_FL_WAIT_L4_CONN|CO_FL_SOCK_WR_SH)))
- shutdown(conn->t.sock.fd, SHUT_RDWR);
+ if (!(conn->flags & CO_FL_WAIT_RD))
+ recv(conn->t.sock.fd, trash.str, trash.size, MSG_NOSIGNAL|MSG_DONTWAIT);
+ setsockopt(conn->t.sock.fd, SOL_SOCKET, SO_LINGER,
+ (struct linger *) &nolinger, sizeof(struct linger));
__conn_data_stop_both(conn);
task_wakeup(t, TASK_WOKEN_IO);
return;