From 6e207e43427ca011e8e048008f0912fc918e2bf2 Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Fri, 14 Apr 2017 09:24:52 -0700 Subject: [PATCH] etcdserver: add 'OnShutdown', stop gRPC server Fix https://github.com/coreos/etcd/issues/7322. Signed-off-by: Gyu-Ho Lee --- etcdserver/config.go | 6 ++++++ etcdserver/server.go | 6 ++++++ 2 files changed, 12 insertions(+) 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()