diff --git a/config/config.go b/config/config.go index 53141fadd093a..2319d953286c7 100644 --- a/config/config.go +++ b/config/config.go @@ -445,6 +445,18 @@ type Status struct { MetricsInterval uint `toml:"metrics-interval" json:"metrics-interval"` ReportStatus bool `toml:"report-status" json:"report-status"` RecordQPSbyDB bool `toml:"record-db-qps" json:"record-db-qps"` + // After a duration of this time in seconds if the server doesn't see any activity it pings + // the client to see if the transport is still alive. + GRPCKeepAliveTime uint `toml:"grpc-keepalive-time" json:"grpc-keepalive-time"` + // After having pinged for keepalive check, the server waits for a duration of timeout in seconds + // and if no activity is seen even after that the connection is closed. + GRPCKeepAliveTimeout uint `toml:"grpc-keepalive-timeout" json:"grpc-keepalive-timeout"` + // The number of max concurrent streams/requests on a client connection. + GRPCConcurrentStreams uint `toml:"grpc-concurrent-streams" json:"grpc-concurrent-streams"` + // Sets window size for stream. The default value is 2MB. + GRPCInitialWindowSize int `toml:"grpc-initial-window-size" json:"grpc-initial-window-size"` + // Set maximum message length in bytes that gRPC can send. `-1` means unlimited. The default value is 10MB. + GRPCMaxSendMsgSize int `toml:"grpc-max-send-msg-size" json:"grpc-max-send-msg-size"` } // Performance is the performance section of the config. @@ -658,11 +670,16 @@ var defaultConf = Config{ EnableSlowLog: *NewAtomicBool(logutil.DefaultTiDBEnableSlowLog), }, Status: Status{ - ReportStatus: true, - StatusHost: DefStatusHost, - StatusPort: DefStatusPort, - MetricsInterval: 15, - RecordQPSbyDB: false, + ReportStatus: true, + StatusHost: DefStatusHost, + StatusPort: DefStatusPort, + MetricsInterval: 15, + RecordQPSbyDB: false, + GRPCKeepAliveTime: 10, + GRPCKeepAliveTimeout: 3, + GRPCConcurrentStreams: 1024, + GRPCInitialWindowSize: 2 * 1024 * 1024, + GRPCMaxSendMsgSize: 10 * 1024 * 1024, }, Performance: Performance{ MaxMemory: 0, diff --git a/config/config_test.go b/config/config_test.go index ab3edc62a0fc4..25c79dc40ebe3 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -261,6 +261,12 @@ deadlock-history-capacity = 123 deadlock-history-collect-retryable = true [top-sql] receiver-address = "127.0.0.1:10100" +[status] +grpc-keepalive-time = 20 +grpc-keepalive-timeout = 10 +grpc-concurrent-streams = 2048 +grpc-initial-window-size = 10240 +grpc-max-send-msg-size = 40960 `) require.NoError(t, err) @@ -318,6 +324,11 @@ receiver-address = "127.0.0.1:10100" require.False(t, conf.Experimental.EnableNewCharset) require.Equal(t, "127.0.0.1:10100", conf.TopSQL.ReceiverAddress) require.True(t, conf.Experimental.AllowsExpressionIndex) + require.Equal(t, uint(20), conf.Status.GRPCKeepAliveTime) + require.Equal(t, uint(10), conf.Status.GRPCKeepAliveTimeout) + require.Equal(t, uint(2048), conf.Status.GRPCConcurrentStreams) + require.Equal(t, 10240, conf.Status.GRPCInitialWindowSize) + require.Equal(t, 40960, conf.Status.GRPCMaxSendMsgSize) err = f.Truncate(0) require.NoError(t, err) diff --git a/server/rpc_server.go b/server/rpc_server.go index 67965ac381f4d..674047781a6bd 100644 --- a/server/rpc_server.go +++ b/server/rpc_server.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "net" + "time" "github.com/pingcap/kvproto/pkg/coprocessor" "github.com/pingcap/kvproto/pkg/diagnosticspb" @@ -34,6 +35,7 @@ import ( "github.com/pingcap/tidb/util/memory" "go.uber.org/zap" "google.golang.org/grpc" + "google.golang.org/grpc/keepalive" "google.golang.org/grpc/peer" ) @@ -46,7 +48,15 @@ func NewRPCServer(config *config.Config, dom *domain.Domain, sm util.SessionMana } }() - s := grpc.NewServer() + s := grpc.NewServer( + grpc.KeepaliveParams(keepalive.ServerParameters{ + Time: time.Duration(config.Status.GRPCKeepAliveTime) * time.Second, + Timeout: time.Duration(config.Status.GRPCKeepAliveTimeout) * time.Second, + }), + grpc.MaxConcurrentStreams(uint32(config.Status.GRPCConcurrentStreams)), + grpc.InitialWindowSize(int32(config.Status.GRPCInitialWindowSize)), + grpc.MaxSendMsgSize(config.Status.GRPCMaxSendMsgSize), + ) rpcSrv := &rpcServer{ DiagnosticsServer: sysutil.NewDiagnosticsServer(config.Log.File.Filename), dom: dom,