-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: graceful shutdown #1963
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
Merged
Merged
feat: graceful shutdown #1963
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,10 +11,12 @@ import ( | |
| "net/http" | ||
| "os" | ||
| "strings" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "github.com/ghodss/yaml" | ||
| grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus" | ||
| "github.com/oklog/run" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| "github.com/sirupsen/logrus" | ||
|
|
@@ -68,6 +70,27 @@ func commandServe() *cobra.Command { | |
| return cmd | ||
| } | ||
|
|
||
| func listenAndShutdownGracefully(logger log.Logger, gr *run.Group, srv *http.Server, name string) error { | ||
| l, err := net.Listen("tcp", srv.Addr) | ||
| if err != nil { | ||
| return fmt.Errorf("listening (%s) on %s: %v", name, srv.Addr, err) | ||
| } | ||
|
|
||
| gr.Add(func() error { | ||
| logger.Infof("listening (%s) on %s", name, srv.Addr) | ||
| return srv.Serve(l) | ||
| }, func(err error) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
| defer cancel() | ||
|
|
||
| logger.Debugf("starting graceful shutdown (%s)", name) | ||
| if err := srv.Shutdown(ctx); err != nil { | ||
| logger.Errorf("graceful shutdown (%s): %v", name, err) | ||
| } | ||
| }) | ||
| return nil | ||
| } | ||
|
|
||
| func runServe(options serveOptions) error { | ||
| configFile := options.config | ||
| configData, err := ioutil.ReadFile(configFile) | ||
|
|
@@ -302,21 +325,25 @@ func runServe(options serveOptions) error { | |
| telemetryServ := http.NewServeMux() | ||
| telemetryServ.Handle("/metrics", promhttp.HandlerFor(prometheusRegistry, promhttp.HandlerOpts{})) | ||
|
|
||
| errc := make(chan error, 3) | ||
| var gr run.Group | ||
| if c.Telemetry.HTTP != "" { | ||
| logger.Infof("listening (http/telemetry) on %s", c.Telemetry.HTTP) | ||
| go func() { | ||
| err := http.ListenAndServe(c.Telemetry.HTTP, telemetryServ) | ||
| errc <- fmt.Errorf("listening on %s failed: %v", c.Telemetry.HTTP, err) | ||
| }() | ||
| telemetrySrv := &http.Server{Addr: c.Telemetry.HTTP, Handler: telemetryServ} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
|
||
| defer telemetrySrv.Close() | ||
| if err := listenAndShutdownGracefully(logger, &gr, telemetrySrv, "http/telemetry"); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if c.Web.HTTP != "" { | ||
| logger.Infof("listening (http) on %s", c.Web.HTTP) | ||
| go func() { | ||
| err := http.ListenAndServe(c.Web.HTTP, serv) | ||
| errc <- fmt.Errorf("listening on %s failed: %v", c.Web.HTTP, err) | ||
| }() | ||
| httpSrv := &http.Server{Addr: c.Web.HTTP, Handler: serv} | ||
|
|
||
| defer httpSrv.Close() | ||
| if err := listenAndShutdownGracefully(logger, &gr, httpSrv, "http"); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if c.Web.HTTPS != "" { | ||
| httpsSrv := &http.Server{ | ||
| Addr: c.Web.HTTPS, | ||
|
|
@@ -328,34 +355,44 @@ func runServe(options serveOptions) error { | |
| }, | ||
| } | ||
|
|
||
| logger.Infof("listening (https) on %s", c.Web.HTTPS) | ||
| go func() { | ||
| err = httpsSrv.ListenAndServeTLS(c.Web.TLSCert, c.Web.TLSKey) | ||
| errc <- fmt.Errorf("listening on %s failed: %v", c.Web.HTTPS, err) | ||
| }() | ||
| defer httpsSrv.Close() | ||
| if err := listenAndShutdownGracefully(logger, &gr, httpsSrv, "https"); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if c.GRPC.Addr != "" { | ||
| logger.Infof("listening (grpc) on %s", c.GRPC.Addr) | ||
| go func() { | ||
| errc <- func() error { | ||
| list, err := net.Listen("tcp", c.GRPC.Addr) | ||
| if err != nil { | ||
| return fmt.Errorf("listening on %s failed: %v", c.GRPC.Addr, err) | ||
| } | ||
| s := grpc.NewServer(grpcOptions...) | ||
| api.RegisterDexServer(s, server.NewAPI(serverConfig.Storage, logger)) | ||
| grpcMetrics.InitializeMetrics(s) | ||
| if c.GRPC.Reflection { | ||
| logger.Info("enabling reflection in grpc service") | ||
| reflection.Register(s) | ||
| } | ||
| err = s.Serve(list) | ||
| return fmt.Errorf("listening on %s failed: %v", c.GRPC.Addr, err) | ||
| }() | ||
| }() | ||
| grpcListener, err := net.Listen("tcp", c.GRPC.Addr) | ||
| if err != nil { | ||
| return fmt.Errorf("listening (grcp) on %s: %w", c.GRPC.Addr, err) | ||
| } | ||
|
|
||
| grpcSrv := grpc.NewServer(grpcOptions...) | ||
| api.RegisterDexServer(grpcSrv, server.NewAPI(serverConfig.Storage, logger)) | ||
|
|
||
| grpcMetrics.InitializeMetrics(grpcSrv) | ||
| if c.GRPC.Reflection { | ||
| logger.Info("enabling reflection in grpc service") | ||
| reflection.Register(grpcSrv) | ||
| } | ||
|
|
||
| gr.Add(func() error { | ||
| logger.Infof("listening (grpc) on %s", c.GRPC.Addr) | ||
| return grpcSrv.Serve(grpcListener) | ||
| }, func(err error) { | ||
| logger.Debugf("starting graceful shutdown (grpc)") | ||
| grpcSrv.GracefulStop() | ||
| }) | ||
| } | ||
|
|
||
| return <-errc | ||
| gr.Add(run.SignalHandler(context.Background(), os.Interrupt, syscall.SIGTERM)) | ||
| if err := gr.Run(); err != nil { | ||
| if _, ok := err.(run.SignalError); !ok { | ||
| return fmt.Errorf("run groups: %w", err) | ||
| } | ||
| logger.Infof("%v, shutdown now", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| var ( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.