I've built services that can gracefully shut down, and my typical design is as follows:
First, maintain an active request counter that you increment when you accept a request and decrement when the request has been fully delivered (including output buffer drained).
Second, implement a TERM signal handler. The handler shuts down the listening socket (so no new connections will be accepted), starts a countdown timer, and waits for the active-request counter to reach 0. When either the countdown timer expires or the active-request counter reaches 0, exit.
Comments
I've built services that can gracefully shut down, and my typical design is as follows:
First, maintain an active request counter that you increment when you accept a request and decrement when the request has been fully delivered (including output buffer drained).
Second, implement a TERM signal handler. The handler shuts down the listening socket (so no new connections will be accepted), starts a countdown timer, and waits for the active-request counter to reach 0. When either the countdown timer expires or the active-request counter reaches 0, exit.