Add Shutdown method to registry.Registry (#4338)

This commit is contained in:
Milos Gajdos 2024-05-01 15:05:44 +01:00 committed by GitHub
commit c8e22f6723
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 9 deletions

View file

@ -81,9 +81,6 @@ var tlsVersions = map[string]uint16{
// defaultLogFormatter is the default formatter to use for logs. // defaultLogFormatter is the default formatter to use for logs.
const defaultLogFormatter = "text" const defaultLogFormatter = "text"
// this channel gets notified when process receives signal. It is global to ease unit testing
var quit = make(chan os.Signal, 1)
// HandlerFunc defines an http middleware // HandlerFunc defines an http middleware
type HandlerFunc func(config *configuration.Configuration, handler http.Handler) http.Handler type HandlerFunc func(config *configuration.Configuration, handler http.Handler) http.Handler
@ -130,6 +127,7 @@ type Registry struct {
config *configuration.Configuration config *configuration.Configuration
app *handlers.App app *handlers.App
server *http.Server server *http.Server
quit chan os.Signal
} }
// NewRegistry creates a new registry from a context and configuration struct. // NewRegistry creates a new registry from a context and configuration struct.
@ -173,6 +171,7 @@ func NewRegistry(ctx context.Context, config *configuration.Configuration) (*Reg
app: app, app: app,
config: config, config: config,
server: server, server: server,
quit: make(chan os.Signal, 1),
}, nil }, nil
} }
@ -313,7 +312,7 @@ func (registry *Registry) ListenAndServe() error {
} }
// setup channel to get notified on SIGTERM signal // setup channel to get notified on SIGTERM signal
signal.Notify(quit, syscall.SIGTERM) signal.Notify(registry.quit, syscall.SIGTERM)
serveErr := make(chan error) serveErr := make(chan error)
// Start serving in goroutine and listen for stop signal in main thread // Start serving in goroutine and listen for stop signal in main thread
@ -324,15 +323,20 @@ func (registry *Registry) ListenAndServe() error {
select { select {
case err := <-serveErr: case err := <-serveErr:
return err return err
case <-quit: case <-registry.quit:
dcontext.GetLogger(registry.app).Info("stopping server gracefully. Draining connections for ", config.HTTP.DrainTimeout) dcontext.GetLogger(registry.app).Info("stopping server gracefully. Draining connections for ", config.HTTP.DrainTimeout)
// shutdown the server with a grace period of configured timeout // shutdown the server with a grace period of configured timeout
c, cancel := context.WithTimeout(context.Background(), config.HTTP.DrainTimeout) c, cancel := context.WithTimeout(context.Background(), config.HTTP.DrainTimeout)
defer cancel() defer cancel()
return registry.server.Shutdown(c) return registry.Shutdown(c)
} }
} }
// Shutdown gracefully shuts down the registry's HTTP server.
func (registry *Registry) Shutdown(ctx context.Context) error {
return registry.server.Shutdown(ctx)
}
func configureDebugServer(config *configuration.Configuration) { func configureDebugServer(config *configuration.Configuration) {
if config.HTTP.Debug.Addr != "" { if config.HTTP.Debug.Addr != "" {
go func(addr string) { go func(addr string) {

View file

@ -103,7 +103,7 @@ func TestGracefulShutdown(t *testing.T) {
fmt.Fprintf(conn, "GET /v2/ ") fmt.Fprintf(conn, "GET /v2/ ")
// send stop signal // send stop signal
quit <- os.Interrupt registry.quit <- os.Interrupt
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
// try connecting again. it shouldn't // try connecting again. it shouldn't
@ -325,7 +325,7 @@ func TestRegistrySupportedCipherSuite(t *testing.T) {
} }
// send stop signal // send stop signal
quit <- os.Interrupt registry.quit <- os.Interrupt
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
@ -369,7 +369,7 @@ func TestRegistryUnsupportedCipherSuite(t *testing.T) {
} }
// send stop signal // send stop signal
quit <- os.Interrupt registry.quit <- os.Interrupt
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }