Skip to content

Commit

Permalink
feat: support max timeout env (#175)
Browse files Browse the repository at this point in the history
Co-Authored-By: Minghan Zhang <[email protected]>
  • Loading branch information
Sh1n3zZ and zmh-program committed Jun 29, 2024
1 parent 3dffa7f commit afb13dd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions globals/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package globals
import (
"net/url"
"strings"
"time"

"github.com/gin-gonic/gin"
)

const ChatMaxThread = 5
const AnonymousMaxThread = 1

var HttpMaxTimeout = 30 * time.Minute

var AllowedOrigins []string

var DebugMode bool
Expand Down
9 changes: 8 additions & 1 deletion utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 4 additions & 6 deletions utils/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
Expand Down

0 comments on commit afb13dd

Please sign in to comment.