admin管理员组

文章数量:1024914

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

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
Share Improve this question asked Nov 18, 2024 at 13:45 JacquesJacques 1461 silver badge11 bronze badges 2
  • nginx is open-source, so why not simply look through its source code to see what it actually does? – Remy Lebeau Commented Nov 18, 2024 at 17:05
  • Because I don't know which of the many functions Nginx performs is responsible for this behavior, and someone here might know exactly what it is. – Jacques Commented Nov 18, 2024 at 18:34
Add a comment  | 

1 Answer 1

Reset to default -2

Nginx achieves zero TIME_WAIT sockets under load testing on Windows by leveraging connection reuse and the "reuseport" feature, which enables multiple worker processes to bind to the same port, distributing load efficiently. Additionally, Nginx uses non-blocking I/O, optimized connection handling, and proper timeout settings to minimize socket exhaustion. By avoiding unnecessary closures and keeping connections alive with keep-alive mechanisms, it reduces the accumulation of TIME_WAIT states under heavy load.

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

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
Share Improve this question asked Nov 18, 2024 at 13:45 JacquesJacques 1461 silver badge11 bronze badges 2
  • nginx is open-source, so why not simply look through its source code to see what it actually does? – Remy Lebeau Commented Nov 18, 2024 at 17:05
  • Because I don't know which of the many functions Nginx performs is responsible for this behavior, and someone here might know exactly what it is. – Jacques Commented Nov 18, 2024 at 18:34
Add a comment  | 

1 Answer 1

Reset to default -2

Nginx achieves zero TIME_WAIT sockets under load testing on Windows by leveraging connection reuse and the "reuseport" feature, which enables multiple worker processes to bind to the same port, distributing load efficiently. Additionally, Nginx uses non-blocking I/O, optimized connection handling, and proper timeout settings to minimize socket exhaustion. By avoiding unnecessary closures and keeping connections alive with keep-alive mechanisms, it reduces the accumulation of TIME_WAIT states under heavy load.

本文标签: cHow does nginx achieve zero TIMEWAIT sockets under load testing on WindowsStack Overflow