Skip to content

Commit

Permalink
force server shutdown on run context done
Browse files Browse the repository at this point in the history
  • Loading branch information
abursavich committed May 23, 2024
1 parent 87b0247 commit 2a7d451
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ type httpServer struct {
}

func (s *httpServer) Run(ctx context.Context) error {
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-done:
case <-ctx.Done():
_ = s.srv.Close()
}
}()
if err := s.srv.Serve(s.lis); err != http.ErrServerClosed {
return err
}
Expand Down Expand Up @@ -64,6 +73,15 @@ func GRPCServerProcess(lis net.Listener, srv GRPCServer) Process {
}

func (s *grpcServer) Run(ctx context.Context) error {
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-done:
case <-ctx.Done():
_ = s.Shutdown(ctx)
}
}()
return s.srv.Serve(s.lis)
}

Expand Down

0 comments on commit 2a7d451

Please sign in to comment.