Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an elegant shutdown mechanism #4672

Open
konglinglong opened this issue Feb 28, 2025 · 1 comment
Open

add an elegant shutdown mechanism #4672

konglinglong opened this issue Feb 28, 2025 · 1 comment

Comments

@konglinglong
Copy link

Is your feature request related to a problem? Please describe.
The current Stop() method of go-zero's HTTP service (rest.Server) only closes the logging module and lacks an elegant shutdown mechanism. This causes port occupation conflicts when running multiple test cases consecutively (e.g., address already in use), as the port is not released properly after tests.

Describe the solution you'd like
Implement the following logic in rest.Server.Stop():

  1. Stop accepting new requests (close the listening port).
  2. Wait for all active requests to complete or timeout.
  3. Release the port and other resources.
  4. Provide a configurable timeout period to avoid infinite waiting.
  5. Reference the implementation of Go's native http.Server.Shutdown().

or signal-triggered graceful shutdown (e.g., SIGTERM)

Describe alternatives you've considered

  1. Manually invoking net/http's graceful shutdown in test code, but this requires intrusive modifications.
  2. Using third-party libraries (e.g., github.com/facebookgo/grace), which adds dependency complexity.
  3. Force-killing processes (e.g., kill -9), but this interrupts active requests.

Additional context
Example of test failure due to port occupation:

func TestServer1(t *testing.T) {
    server := rest.MustNewServer(rest.RestConf{Port: 8080})
    defer server.Stop() // Current Stop() is ineffective; port remains occupied
    // Test logic...
}

func TestServer2(t *testing.T) {
    server := rest.MustNewServer(rest.RestConf{Port: 8080}) // Error: port already in use
    // Test logic...
}
@kevwan
Copy link
Contributor

kevwan commented Mar 1, 2025

The graceful shutdown is implemented in starter.go, and automatically accept the SIGTERM signal to trigger.

Before shutting down, we need to do some cleanup, for example, marking health manager not ready etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants