close_socket_gracefully(): set linger option to prevent port exhaustion

This commit is contained in:
valenok committed 2011-06-24 08:31:37 +01:00
1 parent 71b6a53617
commit a844115043
1 file changed
+7
+7
View File
@@ -3691,8 +3691,15 @@ static void reset_per_request_attributes(struct mg_connection *conn) {
static void close_socket_gracefully(SOCKET sock) {
char buf[BUFSIZ];
struct linger linger;
int n;
// Set linger option to avoid socket hanging out after close. This prevent
// ephemeral port exhaust problem under high QPS.
linger.l_onoff = 1;
linger.l_linger = 1;
setsockopt(sock, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
// Send FIN to the client
(void) shutdown(sock, SHUT_WR);
set_non_blocking_mode(sock);