(addr string, kaEnabled bool, tlsConf *tls.Config)
| 145 | } |
| 146 | |
| 147 | func serveGrpc(addr string, kaEnabled bool, tlsConf *tls.Config) (*grpc.Server, error) { |
| 148 | if addr == "" { |
| 149 | return nil, nil |
| 150 | } |
| 151 | |
| 152 | lis, err := netListener(addr) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | |
| 157 | secure := "" |
| 158 | var opts []grpc.ServerOption |
| 159 | opts = append(opts, grpc.MaxRecvMsgSize(int(globals.maxMessageSize))) |
| 160 | if tlsConf != nil { |
| 161 | opts = append(opts, grpc.Creds(credentials.NewTLS(tlsConf))) |
| 162 | secure = " secure" |
| 163 | } |
| 164 | |
| 165 | if kaEnabled { |
| 166 | kepConfig := keepalive.EnforcementPolicy{ |
| 167 | MinTime: 1 * time.Second, // If a client pings more than once every second, terminate the connection |
| 168 | PermitWithoutStream: true, // Allow pings even when there are no active streams |
| 169 | } |
| 170 | opts = append(opts, grpc.KeepaliveEnforcementPolicy(kepConfig)) |
| 171 | |
| 172 | kpConfig := keepalive.ServerParameters{ |
| 173 | Time: 60 * time.Second, // Ping the client if it is idle for 60 seconds to ensure the connection is still active |
| 174 | Timeout: 20 * time.Second, // Wait 20 second for the ping ack before assuming the connection is dead |
| 175 | } |
| 176 | opts = append(opts, grpc.KeepaliveParams(kpConfig)) |
| 177 | } |
| 178 | |
| 179 | srv := grpc.NewServer(opts...) |
| 180 | pbx.RegisterNodeServer(srv, &grpcNodeServer{}) |
| 181 | logs.Info.Printf("gRPC/%s%s server is registered at [%s]", grpc.Version, secure, addr) |
| 182 | |
| 183 | go func() { |
| 184 | if err := srv.Serve(lis); err != nil { |
| 185 | logs.Err.Println("gRPC server failed:", err) |
| 186 | } |
| 187 | }() |
| 188 | |
| 189 | return srv, nil |
| 190 | } |
no test coverage detected
searching dependent graphs…