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 (
"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

View File

@ -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 {

View File

@ -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},
},