diff --git a/v4/server/grpc/grpc.go b/v4/server/grpc/grpc.go index edb85d73..3ae4cdb2 100644 --- a/v4/server/grpc/grpc.go +++ b/v4/server/grpc/grpc.go @@ -983,9 +983,14 @@ func (g *grpcServer) Start() error { close(exit) }() + gracefulTimeout := time.Second + if v, ok := g.opts.Context.Value(gracefulStopTimeoutKey{}).(time.Duration); ok && v > 0 { + gracefulTimeout = v + } + select { case <-exit: - case <-time.After(time.Second): + case <-time.After(gracefulTimeout): g.srv.Stop() } diff --git a/v4/server/grpc/options.go b/v4/server/grpc/options.go index ca0c199a..a9f40e47 100644 --- a/v4/server/grpc/options.go +++ b/v4/server/grpc/options.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "net" + "time" "go-micro.dev/v4/broker" "go-micro.dev/v4/codec" @@ -22,6 +23,7 @@ type maxMsgSizeKey struct{} type maxConnKey struct{} type tlsAuth struct{} type grpcServerKey struct{} +type gracefulStopTimeoutKey struct{} // gRPC Codec to be used to encode/decode requests for a given content type. func Codec(contentType string, c encoding.Codec) server.Option { @@ -72,6 +74,12 @@ func MaxMsgSize(s int) server.Option { return setServerOption(maxMsgSizeKey{}, s) } +// GracefulStopTimeout set the timeout for the server to stop gracefully +// before forcefully stopping. Default is 1 second. +func GracefulStopTimeout(d time.Duration) server.Option { + return setServerOption(gracefulStopTimeoutKey{}, d) +} + func newOptions(opt ...server.Option) server.Options { opts := server.Options{ Codecs: make(map[string]codec.NewCodec), diff --git a/v5/server/grpc/grpc.go b/v5/server/grpc/grpc.go index e050ae1a..1bd472f6 100644 --- a/v5/server/grpc/grpc.go +++ b/v5/server/grpc/grpc.go @@ -983,9 +983,14 @@ func (g *grpcServer) Start() error { close(exit) }() + gracefulTimeout := time.Second + if v, ok := g.opts.Context.Value(gracefulStopTimeoutKey{}).(time.Duration); ok && v > 0 { + gracefulTimeout = v + } + select { case <-exit: - case <-time.After(time.Second): + case <-time.After(gracefulTimeout): g.srv.Stop() } diff --git a/v5/server/grpc/options.go b/v5/server/grpc/options.go index e9a48a3a..224b5d42 100644 --- a/v5/server/grpc/options.go +++ b/v5/server/grpc/options.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "net" + "time" "go-micro.dev/v5/broker" "go-micro.dev/v5/codec" @@ -22,6 +23,7 @@ type maxMsgSizeKey struct{} type maxConnKey struct{} type tlsAuth struct{} type grpcServerKey struct{} +type gracefulStopTimeoutKey struct{} // gRPC Codec to be used to encode/decode requests for a given content type. func Codec(contentType string, c encoding.Codec) server.Option { @@ -72,6 +74,12 @@ func MaxMsgSize(s int) server.Option { return setServerOption(maxMsgSizeKey{}, s) } +// GracefulStopTimeout set the timeout for the server to stop gracefully +// before forcefully stopping. Default is 1 second. +func GracefulStopTimeout(d time.Duration) server.Option { + return setServerOption(gracefulStopTimeoutKey{}, d) +} + func newOptions(opt ...server.Option) server.Options { opts := server.Options{ Codecs: make(map[string]codec.NewCodec),