Skip to content

Conversation

@amikai
Copy link

@amikai amikai commented Mar 15, 2025

run provides a good way to write a gracefully shutting down HTTP server. It also provides a SignalHandler to make it more elegant. So I want to add this example in the Readme.

What's changed

  • Delete some trailing whitespace in the Readme
  • Add gracefully shutdown http server example

Copy link
Member

@peterbourgon peterbourgon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SignalHandler is orthogonal to the example code that's included in the README, and in any case the package docs should (hopefully!) make clear how to use that particular helper.

I guess it could make sense to add an http.Server.Shutdown example to the README, but it would probably need to look something like my suggestion, here.

Comment on lines +65 to +81
### Gracefully shutdown HTTP server
```go
g := &run.Group{}
g.Add(run.SignalHandler(context.Background(), syscall.SIGINT, syscall.SIGTERM))
srv := &http.Server{}
g.Add(func() error {
log.Printf("starting server")
return srv.ListenAndServe()
}, func(err error) {
log.Printf("shutting down server")
shutdownErr := srv.Shutdown(context.Background())
log.Printf("shutdown err: %v", shutdownErr)
})
err := g.Run()
log.Printf("run err: %v", err)
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Gracefully shutdown HTTP server
```go
g := &run.Group{}
g.Add(run.SignalHandler(context.Background(), syscall.SIGINT, syscall.SIGTERM))
srv := &http.Server{}
g.Add(func() error {
log.Printf("starting server")
return srv.ListenAndServe()
}, func(err error) {
log.Printf("shutting down server")
shutdownErr := srv.Shutdown(context.Background())
log.Printf("shutdown err: %v", shutdownErr)
})
err := g.Run()
log.Printf("run err: %v", err)
```
### http.Server.Shutdown
```go
srv := &http.Server{
Addr: "localhost:8080",
Handler: ...,
}
g.Add(func() error {
return srv.ListenAndServe()
}, func(error) {
ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
defer cancel()
srv.Shutdown(ctx)
})

Copy link
Author

@amikai amikai Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SignalHandler is orthogonal to the example code that's included in the README, and in any case the package docs should (hopefully!) make clear how to use that particular helper.

Yes, you’re right—this SignalHandler is orthogonal to the code in Readme. In my opinion, the examples currently only shows the control flow of run but doesn’t demonstrate the helper (SignalHandler).

I’d like to add it to the README because:

  1. Some users only skim the README to see if it has the features they need before diving into the package documentation for details. That’s why most authors include real-world examples in the README. For instance, in this case, we could show users that run provides a SignalHandler. I believe most users aren’t aware of this feature—at least my colleagues aren’t.
  2. Gracefully shutting down an HTTP server is a commonly used piece of code, and I’d like users to know that run can accomplish this too.

That said, I’m totally okay with your approach as well, and I think it could work just fine. What do you think about proceeding with this?

peterbourgon added a commit that referenced this pull request Jun 27, 2025
peterbourgon added a commit that referenced this pull request Jun 27, 2025
* Update test.yaml

* Fold in README updates from #36 (thanks, @amikai)
@peterbourgon
Copy link
Member

Included in https://github.com/oklog/run/releases/tag/v1.2.0 — thank you!

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

Successfully merging this pull request may close these issues.

2 participants