gRPC: add keepalive option for server

This commit is contained in:
hmol233 2021-07-05 21:25:03 +08:00
parent d9d04a230f
commit 57b9006d26
No known key found for this signature in database
GPG Key ID: D617A9DAB0C992D5

View File

@ -2,9 +2,11 @@ package grpc
import ( import (
"context" "context"
"time"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/net"
@ -75,12 +77,19 @@ func Listen(ctx context.Context, address net.Address, port net.Port, settings *i
config := tls.ConfigFromStreamSettings(settings) config := tls.ConfigFromStreamSettings(settings)
var options []grpc.ServerOption
var s *grpc.Server var s *grpc.Server
if config == nil { if config != nil {
s = grpc.NewServer() options = append(options, grpc.Creds(credentials.NewTLS(config.GetTLSConfig(tls.WithNextProto("h2")))))
} else {
s = grpc.NewServer(grpc.Creds(credentials.NewTLS(config.GetTLSConfig(tls.WithNextProto("h2")))))
} }
if grpcSettings.IdleTimeout > 0 || grpcSettings.HealthCheckTimeout > 0 {
options = append(options, grpc.KeepaliveParams(keepalive.ServerParameters{
Time: time.Second * time.Duration(grpcSettings.IdleTimeout),
Timeout: time.Second * time.Duration(grpcSettings.HealthCheckTimeout),
}))
}
s = grpc.NewServer(options...)
listener.s = s listener.s = s
if settings.SocketSettings != nil && settings.SocketSettings.AcceptProxyProtocol { if settings.SocketSettings != nil && settings.SocketSettings.AcceptProxyProtocol {