Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
added shutdown timeout option
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Mar 6, 2022
1 parent 0a640e1 commit 61fb3fe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type Options struct {
// to "_buffalo_session".
SessionName string `json:"session_name"`

// Timeout in second for ongoing requests when shutdown the server.
// The default value is 60.
TimeoutSecondShutdown int `json:"timeout_second_shutdown"`

// Worker implements the Worker interface and can process tasks in the background.
// Default is "github.com/gobuffalo/worker.Simple.
Worker worker.Worker `json:"-"`
Expand Down Expand Up @@ -99,6 +103,7 @@ func optionsWithDefaults(opts Options) Options {
// TCP case
opts.Addr = defaults.String(opts.Addr, fmt.Sprintf("%s:%s", envAddr, envy.Get("PORT", "3000")))
}
opts.Host = defaults.String(opts.Host, envy.Get("HOST", fmt.Sprintf("http://127.0.0.1:%s", envy.Get("PORT", "3000"))))

if opts.PreWares == nil {
opts.PreWares = []PreWare{}
Expand Down Expand Up @@ -176,12 +181,15 @@ func optionsWithDefaults(opts Options) Options {

opts.SessionStore = cookieStore
}
opts.SessionName = defaults.String(opts.SessionName, "_buffalo_session")

if opts.Worker == nil {
w := worker.NewSimpleWithContext(opts.Context)
w.Logger = opts.Logger
opts.Worker = w
}
opts.SessionName = defaults.String(opts.SessionName, "_buffalo_session")
opts.Host = defaults.String(opts.Host, envy.Get("HOST", fmt.Sprintf("http://127.0.0.1:%s", envy.Get("PORT", "3000"))))

opts.TimeoutSecondShutdown = defaults.Int(opts.TimeoutSecondShutdown, 60)

return opts
}
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (a *App) Serve(srvs ...servers.Server) error {
// shutting down listeners first, to make sure no more new request
a.Logger.Info("shutting down servers")
for _, s := range srvs {
// TODO: implement shutdown timeout as an option
ctx, cfn := context.WithTimeout(context.Background(), 60*time.Second)
timeout := time.Duration(a.Options.TimeoutSecondShutdown) * time.Second
ctx, cfn := context.WithTimeout(context.Background(), timeout)
defer cfn()
if err := s.Shutdown(ctx); err != nil {
a.Logger.Error("shutting down server: ", err)
Expand Down

0 comments on commit 61fb3fe

Please sign in to comment.