diff --git a/etcdserver/config.go b/etcdserver/config.go index 50bc212928ae..b9236612ab75 100644 --- a/etcdserver/config.go +++ b/etcdserver/config.go @@ -61,6 +61,12 @@ type ServerConfig struct { ClientCertAuthEnabled bool AuthToken string + + // OnShutdown gracefully stops gRPC server on shutdown. + // It first stops accepting new connections, RPCs, and + // blocks until all pending RPCs are finished + // TODO: drain other requests + OnShutdown func() } // VerifyBootstrap sanity-checks the initial config for bootstrap case diff --git a/etcdserver/server.go b/etcdserver/server.go index cd95339454ac..564f5b7c4c33 100644 --- a/etcdserver/server.go +++ b/etcdserver/server.go @@ -689,6 +689,12 @@ func (s *EtcdServer) run() { } defer func() { + // stop accepting new connections, RPCs, + // and blocks until all pending RPCs are finished + if s.Cfg.OnShutdown != nil { + s.Cfg.OnShutdown() + } + s.wgMu.Lock() // block concurrent waitgroup adds in goAttach while stopping close(s.stopping) s.wgMu.Unlock()