From afb13dd5453ea0629b088f2f0851043ea714665c Mon Sep 17 00:00:00 2001 From: Deng Junhai Date: Sun, 30 Jun 2024 02:14:59 +0800 Subject: [PATCH] feat: support max timeout env (#175) Co-Authored-By: Minghan Zhang <112773885+zmh-program@users.noreply.github.com> --- globals/variables.go | 3 +++ utils/config.go | 9 ++++++++- utils/net.go | 10 ++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/globals/variables.go b/globals/variables.go index 68b9973..6460cba 100644 --- a/globals/variables.go +++ b/globals/variables.go @@ -3,6 +3,7 @@ package globals import ( "net/url" "strings" + "time" "github.com/gin-gonic/gin" ) @@ -10,6 +11,8 @@ import ( const ChatMaxThread = 5 const AnonymousMaxThread = 1 +var HttpMaxTimeout = 30 * time.Minute + var AllowedOrigins []string var DebugMode bool diff --git a/utils/config.go b/utils/config.go index 8e1fa9f..7ae32ca 100644 --- a/utils/config.go +++ b/utils/config.go @@ -3,10 +3,12 @@ package utils import ( "chat/globals" "fmt" + "strings" + "time" + "github.com/gin-contrib/static" "github.com/gin-gonic/gin" "github.com/spf13/viper" - "strings" ) var configFile = "config/config.yaml" @@ -34,6 +36,11 @@ func ReadConf() { viper.AutomaticEnv() viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + + if timeout := viper.GetInt("max_timeout"); timeout > 0 { + globals.HttpMaxTimeout = time.Second * time.Duration(timeout) + globals.Debug(fmt.Sprintf("[service] http client timeout set to %ds from env", timeout)) + } } func NewEngine() *gin.Engine { diff --git a/utils/net.go b/utils/net.go index b747061..015d95c 100644 --- a/utils/net.go +++ b/utils/net.go @@ -6,22 +6,20 @@ import ( "context" "crypto/tls" "fmt" - "github.com/goccy/go-json" - "golang.org/x/net/proxy" "io" "net" "net/http" "net/url" "runtime/debug" "strings" - "time" -) -var maxTimeout = 30 * time.Minute + "github.com/goccy/go-json" + "golang.org/x/net/proxy" +) func newClient(c []globals.ProxyConfig) *http.Client { client := &http.Client{ - Timeout: maxTimeout, + Timeout: globals.HttpMaxTimeout, Transport: &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, },