Skip to content

Commit

Permalink
Merge pull request #19 from daveads/ci-fix
Browse files Browse the repository at this point in the history
race fix
  • Loading branch information
alixander authored Oct 1, 2024
2 parents 55b3812 + b70cdfa commit 2c8848a
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions xhttp/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,25 @@ func Serve(ctx context.Context, shutdownTimeout time.Duration, s *http.Server, l
return ctx
}

done := make(chan error, 1)
serverClosed := make(chan struct{})
var serverError error
go func() {
done <- s.Serve(l)
serverError = s.Serve(l)
close(serverClosed)
}()

select {
case err := <-done:
return err
case <-serverClosed:
return serverError
case <-ctx.Done():
ctx = xcontext.WithoutCancel(ctx)
ctx, cancel := context.WithTimeout(ctx, shutdownTimeout)
shutdownCtx, cancel := context.WithTimeout(xcontext.WithoutCancel(ctx), shutdownTimeout)
defer cancel()
return s.Shutdown(ctx)

err := s.Shutdown(shutdownCtx)
<-serverClosed // Wait for server to exit
if err != nil {
return err
}
return serverError
}
}

0 comments on commit 2c8848a

Please sign in to comment.