Skip to content

Commit

Permalink
embed: share grpc connection for grpc json services
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Romano authored and gyuho committed Jul 7, 2017
1 parent 8f2b484 commit 1c2c752
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions embed/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,28 +160,38 @@ func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Ha
})
}

type registerHandlerFunc func(context.Context, *gw.ServeMux, string, []grpc.DialOption) error
type registerHandlerFunc func(context.Context, *gw.ServeMux, *grpc.ClientConn) error

func (sctx *serveCtx) registerGateway(opts []grpc.DialOption) (*gw.ServeMux, error) {
ctx := sctx.ctx
addr := sctx.l.Addr().String()
conn, err := grpc.DialContext(ctx, sctx.l.Addr().String(), opts...)
if err != nil {
return nil, err
}
gwmux := gw.NewServeMux()

handlers := []registerHandlerFunc{
etcdservergw.RegisterKVHandlerFromEndpoint,
etcdservergw.RegisterWatchHandlerFromEndpoint,
etcdservergw.RegisterLeaseHandlerFromEndpoint,
etcdservergw.RegisterClusterHandlerFromEndpoint,
etcdservergw.RegisterMaintenanceHandlerFromEndpoint,
etcdservergw.RegisterAuthHandlerFromEndpoint,
v3lockgw.RegisterLockHandlerFromEndpoint,
v3electiongw.RegisterElectionHandlerFromEndpoint,
etcdservergw.RegisterKVHandler,
etcdservergw.RegisterWatchHandler,
etcdservergw.RegisterLeaseHandler,
etcdservergw.RegisterClusterHandler,
etcdservergw.RegisterMaintenanceHandler,
etcdservergw.RegisterAuthHandler,
v3lockgw.RegisterLockHandler,
v3electiongw.RegisterElectionHandler,
}
for _, h := range handlers {
if err := h(ctx, gwmux, addr, opts); err != nil {
if err := h(ctx, gwmux, conn); err != nil {
return nil, err
}
}
go func() {
<-ctx.Done()
if cerr := conn.Close(); cerr != nil {
plog.Warningf("failed to close conn to %s: %v", sctx.l.Addr().String(), cerr)
}
}()

return gwmux, nil
}

Expand Down

0 comments on commit 1c2c752

Please sign in to comment.