Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion v4/server/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
8 changes: 8 additions & 0 deletions v4/server/grpc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"net"
"time"

"go-micro.dev/v4/broker"
"go-micro.dev/v4/codec"
Expand All @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down
7 changes: 6 additions & 1 deletion v5/server/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
8 changes: 8 additions & 0 deletions v5/server/grpc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"net"
"time"

"go-micro.dev/v5/broker"
"go-micro.dev/v5/codec"
Expand All @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down