Skip to content

Commit

Permalink
Gracefully shutdown grpc servers (#4897)
Browse files Browse the repository at this point in the history
## Motivation
Closes #4867 

## Changes
Makes a call to graceful stop before force closing open connections.

## Test Plan
See that CI is passing

## DevOps Notes
<!-- Please uncheck these items as applicable to make DevOps aware of changes that may affect releases -->
- [x] This PR does not require configuration changes (e.g., environment variables, GitHub secrets, VM resources)
- [x] This PR does not affect public APIs
- [x] This PR does not rely on a new version of external services (PoET, elasticsearch, etc.)
- [x] This PR does not make changes to log messages (which monitoring infrastructure may rely on)


Co-authored-by: Bartosz Różański <bartek.roza@gmail.com>
  • Loading branch information
piersy and poszu committed Oct 4, 2023
1 parent f74d4b2 commit 81f79f5
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/grpcserver/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ func (s *Server) Start() error {
// Close stops the server.
func (s *Server) Close() error {
s.logger.Info("stopping the grpc server")
// GracefulStop waits for all connections to be closed before closing the
// server and returning. If there are long running stream connections then
// GracefulStop will never return. So we call it in a background thread,
// wait a bit and then call Stop which will forcefully close any remaining
// connections.
s.grp.Go(func() error {
s.GrpcServer.GracefulStop()
return nil
})
time.Sleep(time.Second * 1)
s.GrpcServer.Stop()
return s.grp.Wait()
}
Expand Down

0 comments on commit 81f79f5

Please sign in to comment.