feat: support max timeout env (#175)

Co-Authored-By: Minghan Zhang <112773885+zmh-program@users.noreply.github.com>
This commit is contained in:
Deng Junhai 2024-06-30 02:14:59 +08:00
parent 3dffa7fdab
commit afb13dd545
3 changed files with 15 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package globals
import ( import (
"net/url" "net/url"
"strings" "strings"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -10,6 +11,8 @@ import (
const ChatMaxThread = 5 const ChatMaxThread = 5
const AnonymousMaxThread = 1 const AnonymousMaxThread = 1
var HttpMaxTimeout = 30 * time.Minute
var AllowedOrigins []string var AllowedOrigins []string
var DebugMode bool var DebugMode bool

View File

@ -3,10 +3,12 @@ package utils
import ( import (
"chat/globals" "chat/globals"
"fmt" "fmt"
"strings"
"time"
"github.com/gin-contrib/static" "github.com/gin-contrib/static"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/spf13/viper" "github.com/spf13/viper"
"strings"
) )
var configFile = "config/config.yaml" var configFile = "config/config.yaml"
@ -34,6 +36,11 @@ func ReadConf() {
viper.AutomaticEnv() viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) 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 { func NewEngine() *gin.Engine {

View File

@ -6,22 +6,20 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"github.com/goccy/go-json"
"golang.org/x/net/proxy"
"io" "io"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"runtime/debug" "runtime/debug"
"strings" "strings"
"time"
)
var maxTimeout = 30 * time.Minute "github.com/goccy/go-json"
"golang.org/x/net/proxy"
)
func newClient(c []globals.ProxyConfig) *http.Client { func newClient(c []globals.ProxyConfig) *http.Client {
client := &http.Client{ client := &http.Client{
Timeout: maxTimeout, Timeout: globals.HttpMaxTimeout,
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}, },