MCPcopy
hub / github.com/grpc/grpc-go / NewServer

Function NewServer

internal/testutils/fakegrpclb/server.go:82–119  ·  view source on GitHub ↗

NewServer creates a new Server with passed in params. Returns a non-nil error if the params are invalid.

(params ServerParams)

Source from the content-addressed store, hash-verified

80// NewServer creates a new Server with passed in params. Returns a non-nil error
81// if the params are invalid.
82func NewServer(params ServerParams) (*Server, error) {
83 var servers []*lbpb.Server
84 for _, addr := range params.BackendAddresses {
85 ipStr, portStr, err := net.SplitHostPort(addr)
86 if err != nil {
87 return nil, fmt.Errorf("failed to parse list of backend address %q: %v", addr, err)
88 }
89 ip, err := netip.ParseAddr(ipStr)
90 if err != nil {
91 return nil, fmt.Errorf("failed to parse ip %q: %v", ipStr, err)
92 }
93 port, err := strconv.Atoi(portStr)
94 if err != nil {
95 return nil, fmt.Errorf("failed to convert port %q to int", portStr)
96 }
97 logger.Infof("Adding backend ip: %q, port: %d to server list", ip.String(), port)
98 servers = append(servers, &lbpb.Server{
99 IpAddress: ip.AsSlice(),
100 Port: int32(port),
101 })
102 }
103
104 lis, err := net.Listen("tcp", "localhost:"+strconv.Itoa(params.ListenPort))
105 if err != nil {
106 return nil, fmt.Errorf("failed to listen on port %q: %v", params.ListenPort, err)
107 }
108
109 return &Server{
110 sOpts: params.ServerOptions,
111 serviceName: params.LoadBalancedServiceName,
112 servicePort: params.LoadBalancedServicePort,
113 shortStream: params.ShortStream,
114 backends: servers,
115 lis: lis,
116 address: lis.Addr().String(),
117 stopped: make(chan struct{}),
118 }, nil
119}
120
121// Serve starts serving the LoadBalancer service on a gRPC server.
122//

Callers 2

mainFunction · 0.92

Calls 4

ErrorfMethod · 0.65
InfofMethod · 0.65
StringMethod · 0.65
AddrMethod · 0.45

Tested by 1