Skip to content

Commit

Permalink
embed: use gRPC server GracefulStop method
Browse files Browse the repository at this point in the history
Fix #7322.

Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
  • Loading branch information
gyuho committed Apr 14, 2017
1 parent cfbc5e5 commit 90babfc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"path/filepath"
"sync"

"google.golang.org/grpc"

"github.com/coreos/etcd/etcdserver"
"github.com/coreos/etcd/etcdserver/api/v2http"
"github.com/coreos/etcd/pkg/cors"
Expand Down Expand Up @@ -137,6 +139,11 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
if err = e.serve(); err != nil {
return
}
e.Server.Cfg.OnShutdown = func() {
for _, sctx := range e.sctxs {
(<-sctx.stopGRPCc)()
}
}
return
}

Expand Down Expand Up @@ -343,6 +350,10 @@ func (e *Etcd) serve() (err error) {
}

func (e *Etcd) errHandler(err error) {
if transport.IsClosedConnError(err) || err == grpc.ErrServerStopped {
plog.Warningf("server stopped with %q", err.Error())
return
}
select {
case <-e.stopc:
return
Expand Down
15 changes: 14 additions & 1 deletion embed/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ type serveCtx struct {

userHandlers map[string]http.Handler
serviceRegister func(*grpc.Server)
stopGRPCc chan func()
}

func newServeCtx() *serveCtx {
ctx, cancel := context.WithCancel(context.Background())
return &serveCtx{ctx: ctx, cancel: cancel, userHandlers: make(map[string]http.Handler)}
return &serveCtx{ctx: ctx, cancel: cancel, userHandlers: make(map[string]http.Handler), stopGRPCc: make(chan func())}
}

// serve accepts incoming connections on the listener l,
Expand All @@ -74,6 +75,12 @@ func (sctx *serveCtx) serve(s *etcdserver.EtcdServer, tlscfg *tls.Config, handle

if sctx.insecure {
gs := v3rpc.Server(s, nil)
sctx.stopGRPCc <- func() {
plog.Warning("gracefully stopping gRPC server")
gs.GracefulStop()
plog.Warning("gracefully stopped gRPC server")
}
close(sctx.stopGRPCc)
v3electionpb.RegisterElectionServer(gs, servElection)
v3lockpb.RegisterLockServer(gs, servLock)
if sctx.serviceRegister != nil {
Expand Down Expand Up @@ -103,6 +110,12 @@ func (sctx *serveCtx) serve(s *etcdserver.EtcdServer, tlscfg *tls.Config, handle

if sctx.secure {
gs := v3rpc.Server(s, tlscfg)
sctx.stopGRPCc <- func() {
plog.Warning("gracefully stopping gRPC server")
gs.GracefulStop()
plog.Warning("gracefully stopped gRPC server")
}
close(sctx.stopGRPCc)
v3electionpb.RegisterElectionServer(gs, servElection)
v3lockpb.RegisterLockServer(gs, servLock)
if sctx.serviceRegister != nil {
Expand Down

0 comments on commit 90babfc

Please sign in to comment.