最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c++ - How does nginx achieve zero TIME_WAIT sockets under load testing on Windows? - Stack Overflow

matteradmin7PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far