8000
Skip to content

Latest commit

 

History

History
249 lines (221 loc) · 8.45 KB

File metadata and controls

249 lines (221 loc) · 8.45 KB
func (s *GRPCServer) loggingServerModeChangeCallback(addr net.Addr, args ServingModeChangeArgs) {
switch args.Mode {
case connectivity.ServingModeServing:
s.logger.Errorf("Listener %q entering mode: %q", addr.String(), args.Mode)
case connectivity.ServingModeNotServing:
s.logger.Errorf("Listener %q entering mode: %q due to error: %v", addr.String(), args.Mode, args.Err)
}
}
// RegisterService registers a service and its implementation to the underlying
// gRPC server. It is called from the IDL generated code. This must be called
// before invoking Serve.
func (s *GRPCServer) RegisterService(sd *grpc.ServiceDesc, ss any) {
s.gs.RegisterService(sd, ss)
}
// GetServiceInfo returns a map from service names to ServiceInfo.
// Service names include the package names, in the form of <package>.<service>.
func (s *GRPCServer) GetServiceInfo() map[string]grpc.ServiceInfo {
return s.gs.GetServiceInfo()
}
// Serve gets the underlying gRPC server to accept incoming connections on the
// listener lis, which is expected to be listening on a TCP port.
//
// A connection to the management server, to receive xDS configuration, is
// initiated here.
//
// Serve will return a non-nil error unless Stop or GracefulStop is called.
func (s *GRPCServer) Serve(lis net.Listener) error {
s.logger.Infof("Serve() passed a net.Listener on %s", lis.Addr().String())
if _, ok := lis.Addr().(*net.TCPAddr); !ok {
return fmt.Errorf("xds: GRPCServer expects listener to return a net.TCPAddr. Got %T", lis.Addr())
}
if s.quit.HasFired() {
return grpc.ErrServerStopped
}
// The server listener resource name template from the bootstrap
// configuration contains a template for the name of the Listener resource
// to subscribe to for a gRPC server. If the token `%s` is present in the
// string, it will be replaced with the server's listening "IP:port" (e.g.,
// "0.0.0.0:8080", "[::]:8080").
cfg := s.xdsC.BootstrapConfig()
name := bootstrap.PopulateResourceTemplate(cfg.ServerListenerResourceNameTemplate(), lis.Addr().String())
// Create a listenerWrapper which handles all functionality required by
// this particular instance of Serve().
lw := server.NewListenerWrapper(server.ListenerWrapperParams{
Listener: lis,
ListenerResourceName: name,
XDSClient: s.xdsC,
ModeCallback: func(addr net.Addr, mode connectivity.ServingMode, err error) {
s.opts.modeCallback(addr, ServingModeChangeArgs{
Mode: mode,
Err: err,
})
},
})
return s.gs.Serve(lw)
}
// Stop stops the underlying gRPC server. It immediately closes all open
// connections. It cancels all active RPCs on the server side and the
// corresponding pending RPCs on the client side will get notified by connection
// errors.
func (s *GRPCServer) Stop() {
s.quit.Fire()
s.gs.Stop()
if s.xdsC != nil {
s.xdsClientClose()
}
}
// GracefulStop stops the underlying gRPC server gracefully. It stops the server
// from accepting new connections and RPCs and blocks until all the pending RPCs
// are finished.
func (s *GRPCServer) GracefulStop() {
s.quit.Fire()
s.gs.GracefulStop()
if s.xdsC != nil {
s.xdsClientClose()
}
}
// xdsUnaryInterceptor is the unary interceptor added to the gRPC server to
// perform any xDS specific functionality on unary RPCs.
func xdsUnaryInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
if err := server.RouteAndProcess(ctx); err != nil {
return nil, err
}
return handler(ctx, req)
}
// xdsStreamInterceptor is the stream interceptor added to the gRPC server to
// perform any xDS specific functionality on streaming RPCs.
func xdsStreamInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
if err := server.RouteAndProcess(ss.Context()); err != nil {
return err
}
return handler(srv, ss)
}
0