NewGRPCServer creates an xDS-enabled gRPC server using the passed in opts. The underlying gRPC server has no service registered and has not started to accept requests yet.
(opts ...grpc.ServerOption)
| 73 | // The underlying gRPC server has no service registered and has not started to |
| 74 | // accept requests yet. |
| 75 | func NewGRPCServer(opts ...grpc.ServerOption) (*GRPCServer, error) { |
| 76 | newOpts := []grpc.ServerOption{ |
| 77 | grpc.ChainUnaryInterceptor(xdsUnaryInterceptor), |
| 78 | grpc.ChainStreamInterceptor(xdsStreamInterceptor), |
| 79 | } |
| 80 | newOpts = append(newOpts, opts...) |
| 81 | s := &GRPCServer{ |
| 82 | gs: newGRPCServer(newOpts...), |
| 83 | quit: grpcsync.NewEvent(), |
| 84 | } |
| 85 | s.handleServerOptions(opts) |
| 86 | |
| 87 | var mrl estats.MetricsRecorder |
| 88 | mrl = istats.NewMetricsRecorderList(nil) |
| 89 | if srv, ok := s.gs.(*grpc.Server); ok { // Will hit in prod but not for testing. |
| 90 | mrl = internal.MetricsRecorderForServer.(func(*grpc.Server) estats.MetricsRecorder)(srv) |
| 91 | } |
| 92 | |
| 93 | // Initializing the xDS client upfront (instead of at serving time) |
| 94 | // simplifies the code by eliminating the need for a mutex to protect the |
| 95 | // xdsC and xdsClientClose fields. |
| 96 | pool := xdsClientPool |
| 97 | if s.opts.clientPoolForTesting != nil { |
| 98 | pool = s.opts.clientPoolForTesting |
| 99 | } |
| 100 | xdsClient, xdsClientClose, err := pool.NewClient(xdsclient.NameForServer, mrl) |
| 101 | if err != nil { |
| 102 | return nil, fmt.Errorf("xDS client creation failed: %v", err) |
| 103 | } |
| 104 | |
| 105 | // Validate the bootstrap configuration for server specific fields. |
| 106 | |
| 107 | // Listener resource name template is mandatory on the server side. |
| 108 | cfg := xdsClient.BootstrapConfig() |
| 109 | if cfg.ServerListenerResourceNameTemplate() == "" { |
| 110 | xdsClientClose() |
| 111 | return nil, errors.New("missing server_listener_resource_name_template in the bootstrap configuration") |
| 112 | } |
| 113 | |
| 114 | s.xdsC = xdsClient |
| 115 | s.xdsClientClose = xdsClientClose |
| 116 | |
| 117 | s.logger = internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(serverPrefix, s)) |
| 118 | s.logger.Infof("Created xds.GRPCServer") |
| 119 | |
| 120 | return s, nil |
| 121 | } |
| 122 | |
| 123 | // handleServerOptions iterates through the list of server options passed in by |
| 124 | // the user, and handles the xDS server specific options. |