From a00ff2e1151b8822dacdce803bc890d72896be37 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 21 Jan 2025 16:48:51 +0800 Subject: [PATCH] support setting keepalive for server Signed-off-by: Ryan Leung --- server/config/config.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/server/config/config.go b/server/config/config.go index 62aec798e8c..88de2cfa176 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -140,6 +140,14 @@ type Config struct { MaxRequestBytes uint `toml:"max-request-bytes" json:"max-request-bytes"` + // GRPCKeepAliveInterval is the frequency of server-to-client ping + // to check if a connection is alive. Close a non-responsive connection + // after an additional duration of Timeout. 0 to disable. + GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"` + // GRPCKeepAliveTimeout is the additional duration of wait + // before closing a non-responsive connection. 0 to disable. + GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"` + Security configutil.SecurityConfig `toml:"security" json:"security"` LabelProperty LabelPropertyConfig `toml:"label-property" json:"label-property"` @@ -182,6 +190,9 @@ const ( // then 150MB can fit for store reports that have about 300k regions which is something of a huge amount of region on one TiKV. defaultMaxRequestBytes = uint(150 * units.MiB) // 150MB + defaultGRPCKeepAliveInterval = 10 * time.Second + defaultGRPCKeepAliveTimeout = 3 * time.Second + defaultName = "pd" defaultClientUrls = "http://127.0.0.1:2379" defaultPeerUrls = "http://127.0.0.1:2380" @@ -422,6 +433,12 @@ func (c *Config) Adjust(meta *toml.MetaData, reloading bool) error { if !configMetaData.IsDefined("max-request-bytes") { c.MaxRequestBytes = defaultMaxRequestBytes } + if !configMetaData.IsDefined("grpc-keepalive-interval") { + c.GRPCKeepAliveInterval = defaultGRPCKeepAliveInterval + } + if !configMetaData.IsDefined("grpc-keepalive-timeout") { + c.GRPCKeepAliveTimeout = defaultGRPCKeepAliveTimeout + } configutil.AdjustDuration(&c.TickInterval, defaultTickInterval) configutil.AdjustDuration(&c.ElectionInterval, defaultElectionInterval) @@ -694,6 +711,8 @@ func (c *Config) GenEmbedEtcdConfig() (*embed.Config, error) { cfg.AutoCompactionRetention = c.AutoCompactionRetention cfg.QuotaBackendBytes = int64(c.QuotaBackendBytes) cfg.MaxRequestBytes = c.MaxRequestBytes + cfg.GRPCKeepAliveInterval = c.GRPCKeepAliveInterval + cfg.GRPCKeepAliveTimeout = c.GRPCKeepAliveTimeout cfg.ClientTLSInfo.ClientCertAuth = len(c.Security.CAPath) != 0 cfg.ClientTLSInfo.TrustedCAFile = c.Security.CAPath