From bf0c3016cb09332097f12ff8453e4a20a0853cee Mon Sep 17 00:00:00 2001 From: Evstigneev Denis Date: Thu, 29 Feb 2024 13:28:27 +0300 Subject: [PATCH] add gracefultimeout in server --- server/options.go | 9 +++++++++ server/server.go | 2 ++ 2 files changed, 11 insertions(+) diff --git a/server/options.go b/server/options.go index 143f7eeb..8cdccc5b 100644 --- a/server/options.go +++ b/server/options.go @@ -65,6 +65,8 @@ type Options struct { DeregisterAttempts int // Hooks may contains HandleWrapper or Server func wrapper Hooks options.Hooks + // GracefulTimeout timeout for graceful stop server + GracefulTimeout time.Duration } // NewOptions returns new options struct with default or passed values @@ -84,6 +86,7 @@ func NewOptions(opts ...options.Option) Options { Name: DefaultName, Version: DefaultVersion, ID: id.Must(), + GracefulTimeout: DefaultGracefulTimeout, } for _, o := range opts { @@ -162,6 +165,12 @@ func Listener(nl net.Listener) options.Option { } } +func GracefulTimeout(t time.Duration) options.Option { + return func(src interface{}) error { + return options.Set(src, t, ".GracefulTimeout") + } +} + // HandleOptions struct type HandleOptions struct { // Context holds external options diff --git a/server/server.go b/server/server.go index ffeb3c70..c5d46146 100644 --- a/server/server.go +++ b/server/server.go @@ -32,6 +32,8 @@ var ( DefaultMaxMsgRecvSize = 1024 * 1024 * 4 // 4Mb // DefaultMaxMsgSendSize holds default max send size DefaultMaxMsgSendSize = 1024 * 1024 * 4 // 4Mb + // DefaultGracefulTimeout default time for graceful stop + DefaultGracefulTimeout = 5 * time.Second ) // Server is a simple micro server abstraction