mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 02:16:28 +02:00
* chore(deps): bump golang from `a6b787c` to `1415bb0` Bumps golang from `a6b787c` to `1415bb0`. --- updated-dependencies: - dependency-name: golang dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * fix flaky test --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Caleb Doxsey <cdoxsey@pomerium.com>
43 lines
828 B
Go
43 lines
828 B
Go
package grpcutil
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// ServeWithGracefulStop serves the gRPC listener until ctx.Done(), and then gracefully stops and waits for gracefulTimeout
|
|
// before definitively stopping.
|
|
func ServeWithGracefulStop(ctx context.Context, srv *grpc.Server, li net.Listener, gracefulTimeout time.Duration) error {
|
|
go func() {
|
|
// wait for the context to complete
|
|
<-ctx.Done()
|
|
|
|
sctx, stopped := context.WithCancel(context.Background())
|
|
go func() {
|
|
srv.GracefulStop()
|
|
stopped()
|
|
}()
|
|
|
|
wait := time.NewTimer(gracefulTimeout)
|
|
defer wait.Stop()
|
|
|
|
select {
|
|
case <-wait.C:
|
|
case <-sctx.Done():
|
|
return
|
|
}
|
|
|
|
// finally stop it completely
|
|
srv.Stop()
|
|
}()
|
|
|
|
err := srv.Serve(li)
|
|
if errors.Is(err, grpc.ErrServerStopped) {
|
|
err = nil
|
|
}
|
|
return err
|
|
}
|