Skip to content

Commit

Permalink
fix: avoid deadlock if continuous stream of logs are received
Browse files Browse the repository at this point in the history
Previous implementation would not start the shutdown process until no incoming http request is received. The issue is that, in case a constant stream of events, this would deadlock and never refuse new connections.

Signed-off-by: grouville <[email protected]>
  • Loading branch information
grouville committed Aug 3, 2023
1 parent eabb86d commit 529141d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ func serve(ctx context.Context, logger *zap.SugaredLogger) (err error) {
cancel()
}()

// Wait for in-flight requests to complete before shutting down
wg.Wait()

if err = srv.Shutdown(ctxShutDown); err != nil {
logger.Fatalf("http server shutdown failed:%+s", err)
}

// Wait for in-flight requests to complete before shutting down
wg.Wait()

logger.Infof("http server shutdown gracefully")

if err == http.ErrServerClosed {
Expand Down
6 changes: 3 additions & 3 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ loop:
<-shutdownMessage
log.Println("Shutting down server...")

// Wait for all requests to be processed
wg.Wait()

if err := server.Shutdown(context.Background()); err != nil {
log.Fatalf("Server Shutdown: %v", err)
}

// Wait for all requests to be processed
wg.Wait()
}()
}

Expand Down

0 comments on commit 529141d

Please sign in to comment.