Increase timeouts

This commit is contained in:
世界 2024-10-30 13:09:05 +08:00
parent 419058f466
commit 8c143feec8
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
6 changed files with 12 additions and 10 deletions

View file

@ -81,7 +81,7 @@ func NewDefault(router adapter.Router, options option.DialerOptions) (*DefaultDi
if options.ConnectTimeout != 0 { if options.ConnectTimeout != 0 {
dialer.Timeout = time.Duration(options.ConnectTimeout) dialer.Timeout = time.Duration(options.ConnectTimeout)
} else { } else {
dialer.Timeout = C.TCPTimeout dialer.Timeout = C.TCPConnectTimeout
} }
// TODO: Add an option to customize the keep alive period // TODO: Add an option to customize the keep alive period
dialer.KeepAlive = C.TCPKeepAliveInitial dialer.KeepAlive = C.TCPKeepAliveInitial

View file

@ -8,6 +8,7 @@ import (
"sync" "sync"
"time" "time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network" N "github.com/sagernet/sing/common/network"
@ -113,6 +114,7 @@ func URLTest(ctx context.Context, link string, detour N.Dialer) (t uint16, err e
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse return http.ErrUseLastResponse
}, },
Timeout: C.TCPTimeout,
} }
defer client.CloseIdleConnections() defer client.CloseIdleConnections()
resp, err := client.Do(req.WithContext(ctx)) resp, err := client.Do(req.WithContext(ctx))

View file

@ -5,7 +5,8 @@ import "time"
const ( const (
TCPKeepAliveInitial = 10 * time.Minute TCPKeepAliveInitial = 10 * time.Minute
TCPKeepAliveInterval = 75 * time.Second TCPKeepAliveInterval = 75 * time.Second
TCPTimeout = 5 * time.Second TCPConnectTimeout = 5 * time.Second
TCPTimeout = 15 * time.Second
ReadPayloadTimeout = 300 * time.Millisecond ReadPayloadTimeout = 300 * time.Millisecond
DNSTimeout = 10 * time.Second DNSTimeout = 10 * time.Second
QUICTimeout = 30 * time.Second QUICTimeout = 30 * time.Second

View file

@ -9,9 +9,9 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
@ -60,7 +60,7 @@ func (s *Server) downloadExternalUI() error {
httpClient := &http.Client{ httpClient := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
ForceAttemptHTTP2: true, ForceAttemptHTTP2: true,
TLSHandshakeTimeout: 5 * time.Second, TLSHandshakeTimeout: C.TCPTimeout,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return detour.DialContext(ctx, network, M.ParseSocksaddr(addr)) return detour.DialContext(ctx, network, M.ParseSocksaddr(addr))
}, },

View file

@ -17,8 +17,8 @@ import (
"os" "os"
"strconv" "strconv"
"sync" "sync"
"time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/bufio" "github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
@ -69,8 +69,9 @@ type httpClient struct {
func NewHTTPClient() HTTPClient { func NewHTTPClient() HTTPClient {
client := new(httpClient) client := new(httpClient)
client.client.Timeout = 15 * time.Second
client.client.Transport = &client.transport client.client.Transport = &client.transport
client.transport.ForceAttemptHTTP2 = true
client.transport.TLSHandshakeTimeout = C.TCPTimeout
client.transport.TLSClientConfig = &client.tls client.transport.TLSClientConfig = &client.tls
client.transport.DisableKeepAlives = true client.transport.DisableKeepAlives = true
return client return client
@ -127,7 +128,6 @@ func (c *httpClient) TrySocks5(port int32) {
} }
func (c *httpClient) KeepAlive() { func (c *httpClient) KeepAlive() {
c.transport.ForceAttemptHTTP2 = true
c.transport.DisableKeepAlives = false c.transport.DisableKeepAlives = false
} }

View file

@ -7,7 +7,6 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"time"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/geoip" "github.com/sagernet/sing-box/common/geoip"
@ -158,7 +157,7 @@ func (r *Router) downloadGeoIPDatabase(savePath string) error {
httpClient := &http.Client{ httpClient := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
ForceAttemptHTTP2: true, ForceAttemptHTTP2: true,
TLSHandshakeTimeout: 5 * time.Second, TLSHandshakeTimeout: C.TCPTimeout,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return detour.DialContext(ctx, network, M.ParseSocksaddr(addr)) return detour.DialContext(ctx, network, M.ParseSocksaddr(addr))
}, },
@ -213,7 +212,7 @@ func (r *Router) downloadGeositeDatabase(savePath string) error {
httpClient := &http.Client{ httpClient := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
ForceAttemptHTTP2: true, ForceAttemptHTTP2: true,
TLSHandshakeTimeout: 5 * time.Second, TLSHandshakeTimeout: C.TCPTimeout,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return detour.DialContext(ctx, network, M.ParseSocksaddr(addr)) return detour.DialContext(ctx, network, M.ParseSocksaddr(addr))
}, },