func setUpSignerClient(t *testing.T, grpcServer *grpc.Server) (*client.NotarySigner, *grpc.ClientConn, func()) { socketFile, err := ioutil.TempFile("", "notary-grpc-test") require.NoError(t, err) socketFile.Close() os.Remove(socketFile.Name()) lis, err := net.Listen("unix", socketFile.Name()) require.NoError(t, err, "unable to open socket to listen") go grpcServer.Serve(lis) // client setup clientConn, err := grpc.Dial(socketFile.Name(), grpc.WithInsecure(), grpc.WithDialer(socketDialer)) require.NoError(t, err, "unable to connect to socket as a GRPC client") signerClient := client.NewNotarySigner(clientConn) cleanup := func() { clientConn.Close() grpcServer.Stop() os.Remove(socketFile.Name()) } return signerClient, clientConn, cleanup }
func serveGRPC(server *grpc.Server, l net.Listener) { defer l.Close() if err := server.Serve(l); err != nil { l.Close() logrus.WithError(err).Fatal("containerd: GRPC server failure") } }
// PreregisterServices takes a gRPC server and pre-initializes all counters to 0. // This allows for easier monitoring in Prometheus (no missing metrics), and should be called *after* all services have // been registered with the server. func Register(server *grpc.Server) { serviceInfo := server.GetServiceInfo() for serviceName, info := range serviceInfo { for _, mInfo := range info.Methods { preRegisterMethod(serviceName, &mInfo) } } }
func startGRPC(srv *grpc.Server) { l, err := net.Listen("tcp", *grpcListeningAddr) if err != nil { log.Fatalf("Error listening on GRPC address %q: %v", *grpcListeningAddr, err) } log.Printf("GRPC server listening on %s", l.Addr()) log.Fatal(srv.Serve(l)) }
// GRPCHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC // connections or otherHandler otherwise. func GRPCHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if IsGRPCRequest(r) { grpcServer.ServeHTTP(w, r) } else { otherHandler.ServeHTTP(w, r) } }) }
// grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC // connections or otherHandler otherwise. Copied from cockroachdb. func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { grpcServer.ServeHTTP(w, r) } else { otherHandler.ServeHTTP(w, r) } }) }
// grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC // connections or otherHandler otherwise. Copied from cockroachdb. func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // This is a partial recreation of gRPC's internal checks: // https://github.com/grpc/grpc-go/blob/7834b974e55fbf85a5b01afb5821391c71084efd/transport/handler_server.go#L61 if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { grpcServer.ServeHTTP(w, r) } else { otherHandler.ServeHTTP(w, r) } }) }
// GRPCHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC // connections or otherHandler otherwise. func GRPCHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // TODO(tamird): point to merged gRPC code rather than a PR. // This is a partial recreation of gRPC's internal checks https://github.com/grpc/grpc-go/pull/514/files#diff-95e9a25b738459a2d3030e1e6fa2a718R61 if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") { grpcServer.ServeHTTP(w, r) } else { otherHandler.ServeHTTP(w, r) } }) }
func waitShutdown(rpcSrv *grpc.Server, apiSrv *server.ApiServer) { sigCh := make(chan os.Signal) signal.Notify(sigCh, os.Interrupt, os.Kill) // Block until a signal is received or we got an error select { case signal := <-sigCh: log.Printf("Got signal %s, waiting for shutdown...", signal) rpcSrv.Stop() apiSrv.Shutdown() return } }
// NewCommInstanceWithServer creates a comm instance that creates an underlying gRPC server func NewCommInstanceWithServer(port int, idMapper identity.Mapper, peerIdentity api.PeerIdentityType, dialOpts ...grpc.DialOption) (Comm, error) { var ll net.Listener var s *grpc.Server var secOpt grpc.DialOption if len(dialOpts) == 0 { dialOpts = []grpc.DialOption{grpc.WithTimeout(dialTimeout)} } if port > 0 { s, ll, secOpt = createGRPCLayer(port) dialOpts = append(dialOpts, secOpt) } commInst := &commImpl{ PKIID: idMapper.GetPKIidOfCert(peerIdentity), idMapper: idMapper, logger: util.GetLogger(util.LOGGING_COMM_MODULE, fmt.Sprintf("%d", port)), peerIdentity: peerIdentity, opts: dialOpts, port: port, lsnr: ll, gSrv: s, msgPublisher: NewChannelDemultiplexer(), lock: &sync.RWMutex{}, deadEndpoints: make(chan common.PKIidType, 100), stopping: int32(0), exitChan: make(chan struct{}, 1), subscriptions: make([]chan ReceivedMessage, 0), blackListedPKIIDs: make([]common.PKIidType, 0), } commInst.connStore = newConnStore(commInst, commInst.logger) commInst.idMapper.Put(idMapper.GetPKIidOfCert(peerIdentity), peerIdentity) if port > 0 { go func() { commInst.stopWG.Add(1) defer commInst.stopWG.Done() s.Serve(ll) }() proto.RegisterGossipServer(s, commInst) } commInst.logger.SetLevel(logging.WARNING) return commInst, nil }
// ListenAndServeGRPC creates a listener and serves the specified grpc Server // on it, closing the listener when signalled by the stopper. func ListenAndServeGRPC(stopper *stop.Stopper, server *grpc.Server, addr net.Addr) (net.Listener, error) { ln, err := net.Listen(addr.Network(), addr.String()) if err != nil { return ln, err } stopper.RunWorker(func() { <-stopper.ShouldDrain() server.Stop() }) stopper.RunWorker(func() { FatalIfUnexpected(server.Serve(ln)) }) return ln, nil }
// ListenAndServeGRPC creates a listener and serves server on it, closing // the listener when signalled by the stopper. func ListenAndServeGRPC(stopper *stop.Stopper, server *grpc.Server, addr net.Addr, config *tls.Config) (net.Listener, error) { ln, err := util.Listen(addr, config) if err != nil { return nil, err } stopper.RunWorker(func() { if err := server.Serve(ln); err != nil && !util.IsClosedConnection(err) { log.Fatal(err) } }) stopper.RunWorker(func() { <-stopper.ShouldDrain() if err := ln.Close(); err != nil { log.Fatal(err) } }) return ln, nil }
func RegisterDatastoreServer(s *grpc.Server, srv DatastoreServer) { s.RegisterService(&_Datastore_serviceDesc, srv) }
func RegisterDeviceManagerServer(s *grpc.Server, srv DeviceManagerServer) { s.RegisterService(&_DeviceManager_serviceDesc, srv) }
func RegisterThrottlerServer(s *grpc.Server, srv ThrottlerServer) { s.RegisterService(&_Throttler_serviceDesc, srv) }
func RegisterPropServer(s *grpc.Server, srv PropServer) { s.RegisterService(&_Prop_serviceDesc, srv) }
func RegisterAutomationServer(s *grpc.Server, srv AutomationServer) { s.RegisterService(&_Automation_serviceDesc, srv) }
func RegisterEventsServer(s *grpc.Server, srv EventsServer) { s.RegisterService(&_Events_serviceDesc, srv) }
func RegisterTxnSvcServer(s *grpc.Server, srv TxnSvcServer) { s.RegisterService(&_TxnSvc_serviceDesc, srv) }
func RegisterMysqlCtlServer(s *grpc.Server, srv MysqlCtlServer) { s.RegisterService(&_MysqlCtl_serviceDesc, srv) }
func RegisterHealthServer(s *grpc.Server, srv HealthServer) { s.RegisterService(&_Health_serviceDesc, srv) }
func RegisterRouteGuideServer(s *grpc.Server, srv RouteGuideServer) { s.RegisterService(&_RouteGuide_serviceDesc, srv) }
func RegisterDopplerServer(s *grpc.Server, srv DopplerServer) { s.RegisterService(&_Doppler_serviceDesc, srv) }
func RegisterGeoServer(s *grpc.Server, srv GeoServer) { s.RegisterService(&_Geo_serviceDesc, srv) }
func RegisterFlowCombinationServer(s *grpc.Server, srv FlowCombinationServer) { s.RegisterService(&_FlowCombination_serviceDesc, srv) }
func RegisterBigtableTableServiceServer(s *grpc.Server, srv BigtableTableServiceServer) { s.RegisterService(&_BigtableTableService_serviceDesc, srv) }
func RegisterLoggingServiceV2Server(s *grpc.Server, srv LoggingServiceV2Server) { s.RegisterService(&_LoggingServiceV2_serviceDesc, srv) }
func RegisterWordFilterServiceServer(s *grpc.Server, srv WordFilterServiceServer) { s.RegisterService(&_WordFilterService_serviceDesc, srv) }
func RegisterYourServiceServer(s *grpc.Server, srv YourServiceServer) { s.RegisterService(&_YourService_serviceDesc, srv) }
func RegisterGameServiceServer(s *grpc.Server, srv GameServiceServer) { s.RegisterService(&_GameService_serviceDesc, srv) }