embed: share grpc connection for grpc json services
@@ -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
}
