فهرست منبع

close_socket_gracefully(): set linger option to prevent port exhaustion

valenok 14 سال پیش
والد
کامیت
a844115043
1فایلهای تغییر یافته به همراه7 افزوده شده و 0 حذف شده
  1. 7 0
      mongoose.c

+ 7 - 0
mongoose.c

@@ -3691,8 +3691,15 @@ static void reset_per_request_attributes(struct mg_connection *conn) {
 
 
 static void close_socket_gracefully(SOCKET sock) {
 static void close_socket_gracefully(SOCKET sock) {
   char buf[BUFSIZ];
   char buf[BUFSIZ];
+  struct linger linger;
   int n;
   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
   // Send FIN to the client
   (void) shutdown(sock, SHUT_WR);
   (void) shutdown(sock, SHUT_WR);
   set_non_blocking_mode(sock);
   set_non_blocking_mode(sock);