From fbf3097410a5986f041ea327ced7d7f1dd752d56 Mon Sep 17 00:00:00 2001 From: codchen Date: Mon, 24 Jun 2024 09:22:38 +0800 Subject: [PATCH] Make RPC timeout configurable (#238) --- config/config.go | 5 +++++ config/toml.go | 3 +++ internal/rpc/core/env.go | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/config/config.go b/config/config.go index 27085797e..538f191fb 100644 --- a/config/config.go +++ b/config/config.go @@ -519,6 +519,9 @@ type RPCConfig struct { // Lag threshold determines the threshold for whether the /lag_status endpoint returns OK or not LagThreshold int64 `mapstructure:"lag-threshold"` + + // Timeout for any read request + TimeoutRead time.Duration `mapstructure:"timeout-read"` } // DefaultRPCConfig returns a default configuration for the RPC server @@ -547,6 +550,8 @@ func DefaultRPCConfig() *RPCConfig { TLSCertFile: "", TLSKeyFile: "", LagThreshold: 300, + + TimeoutRead: 10 * time.Second, } } diff --git a/config/toml.go b/config/toml.go index ae0667010..0dceb12e5 100644 --- a/config/toml.go +++ b/config/toml.go @@ -277,6 +277,9 @@ tls-key-file = "{{ .RPC.TLSKeyFile }}" # pprof listen address (https://golang.org/pkg/net/http/pprof) pprof-laddr = "{{ .RPC.PprofListenAddress }}" +# timeout for any read request +timeout-read = "{{ .RPC.TimeoutRead }}" + ####################################################### ### P2P Configuration Options ### ####################################################### diff --git a/internal/rpc/core/env.go b/internal/rpc/core/env.go index 6ca4444f4..a5943c9ad 100644 --- a/internal/rpc/core/env.go +++ b/internal/rpc/core/env.go @@ -243,6 +243,10 @@ func (env *Environment) StartService(ctx context.Context, conf *config.Config) ( cfg.WriteTimeout = conf.RPC.TimeoutBroadcastTxCommit + 1*time.Second } + if conf.RPC.TimeoutRead > 0 { + cfg.ReadTimeout = conf.RPC.TimeoutRead + } + // If the event log is enabled, subscribe to all events published to the // event bus, and forward them to the event log. if lg := env.EventLog; lg != nil {