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

[FEATURE REQUEST] Add method *Server.Shutdown #7

Open
zyxkad opened this issue Jun 18, 2022 · 4 comments
Open

[FEATURE REQUEST] Add method *Server.Shutdown #7

zyxkad opened this issue Jun 18, 2022 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@zyxkad
Copy link

zyxkad commented Jun 18, 2022

Then we can shutdown it gracefully

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

select {
case <-sigs:
	timeoutCtx, cancel := context.WithTimeout(context.Background(), 16 * time.Second)
	defer cancel()
	// do some thing
	server.Shutdown(timeoutCtx)
}
@Dorbmon
Copy link

Dorbmon commented May 7, 2023

Yes, I need this shutdown function too.

@the-hotmann
Copy link

This actually would be very handy.
Love the approach and would like to see it beeing implemented.

@soluchok
Copy link

You do not need a shutdown function to close the server. You can use the following code to achieve graceful shutdown.

// Create your own listener
l, err := net.Listen("tcp", ":7777")
if err != nil {
  // handle err
}

// Start a goroutine to listen to a context and close your listener when context is done
go func() {
  select {
  case <-ctx.Done():
    l.Close()
  }
}()

if err := server.Serve(l); err != nil {
  // handle err
}

@zyxkad
Copy link
Author

zyxkad commented Jan 20, 2024

  1. The shutdown method's context is created & passed when calling it, not before serve it
  2. Shutdown method should detect if there are active connection, if not, it should close immediately, else it will wait until the context is done

@thinkgos thinkgos added enhancement New feature or request good first issue Good for newcomers labels Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

5 participants