c++ – How does nginx achieve zero TIME_WAIT sockets under load testing on Windows?

While implementing a C++ HTTP server on Windows, I’m investigating how nginx handles socket cleanup. Using netstat, I observe that after handling multiple concurrent requests with hey benchmarking tool, nginx maintains zero sockets in TIME_WAIT state.

My understanding of TCP socket states:

  • TIME_WAIT occurs on the connection-terminating endpoint
  • When server initiates close, its socket enters TIME_WAIT
  • When client initiates close, client socket enters TIME_WAIT
  • TIME_WAIT typically lasts 2MSL (Maximum Segment Lifetime)

My current implementation:

  • Standard Windows socket API (Winsock2) with I/O CP
  • Proper socket cleanup with closesocket()
  • SO_REUSEADDR enabled
  • Basic HTTP request handling

Despite these measures, my server accumulates sockets in TIME_WAIT when closing connections. However, nginx somehow avoids this entirely.

What techniques or socket configurations does nginx employ to achieve zero TIME_WAIT sockets? Are there specific Windows-related optimizations in play?

I’ve already investigated:

  • Connection keepalive settings
  • SO_LINGER socket options
  • TCP_NODELAY
  • Various client-initiated close strategies

Read more here: Source link