Skip to content

Commit

Permalink
Refactor Shutdown method so that it blocks
Browse files Browse the repository at this point in the history
Just sending a signal to the quit channel leaves the embedding app with
no easy way of waiting for it to actually shutdown

Signed-off-by: Robin Ketelbuters <robin.ketelbuters@gmail.com>
  • Loading branch information
robinkb committed Apr 28, 2024
1 parent a34e501 commit ab13981
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,16 @@ func (registry *Registry) ListenAndServe() error {
return err
case <-registry.quit:
dcontext.GetLogger(registry.app).Info("stopping server gracefully. Draining connections for ", config.HTTP.DrainTimeout)
// shutdown the server with a grace period of configured timeout
c, cancel := context.WithTimeout(context.Background(), config.HTTP.DrainTimeout)
defer cancel()
return registry.server.Shutdown(c)
return registry.Shutdown()
}
}

// Shutdown gracefully shuts down the registry's HTTP server.
func (registry *Registry) Shutdown() {
registry.quit <- os.Interrupt
func (registry *Registry) Shutdown() error {
// shutdown the server with a grace period of configured timeout
c, cancel := context.WithTimeout(context.Background(), registry.config.HTTP.DrainTimeout)
defer cancel()
return registry.server.Shutdown(c)
}

func configureDebugServer(config *configuration.Configuration) {
Expand Down
6 changes: 3 additions & 3 deletions registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestGracefulShutdown(t *testing.T) {
fmt.Fprintf(conn, "GET /v2/ ")

// send stop signal
registry.Shutdown()
registry.quit <- os.Interrupt
time.Sleep(100 * time.Millisecond)

// try connecting again. it shouldn't
Expand Down Expand Up @@ -325,7 +325,7 @@ func TestRegistrySupportedCipherSuite(t *testing.T) {
}

// send stop signal
registry.Shutdown()
registry.quit <- os.Interrupt
time.Sleep(100 * time.Millisecond)
}

Expand Down Expand Up @@ -369,7 +369,7 @@ func TestRegistryUnsupportedCipherSuite(t *testing.T) {
}

// send stop signal
registry.Shutdown()
registry.quit <- os.Interrupt
time.Sleep(100 * time.Millisecond)
}

Expand Down

0 comments on commit ab13981

Please sign in to comment.