fix: disable keepalive on most outgoing HTTP connections (#10891)

Where we don't expect connection reuse for further requests this reduces
the number of open connection server-side.

Signed-off-by: Jakob Borg <[email protected]>
This commit is contained in:
Jakob Borg authored and GitHub committed 2026-09-22 14:32:44 +02:00
1 parent bb19c017ae
commit e7b8efb410
5 files changed
+40 -11

No files matched your search

+15 -2
View File
@@ -21,6 +21,8 @@ import (
"github.com/syncthing/syncthing/internal/slogutil" "github.com/syncthing/syncthing/internal/slogutil"
"github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/dialer"
"github.com/syncthing/syncthing/lib/tlsutil"
) )
const ( const (
@@ -28,6 +30,17 @@ const (
putRequestTimeout = time.Minute putRequestTimeout = time.Minute
) )
// The client used for uploading crash reports. Crash reports are rare, so
// the connection is not kept open after an upload.
var crashReportClient = &http.Client{
Transport: &http.Transport{
DialContext: dialer.DialContext,
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
TLSClientConfig: tlsutil.SecureDefaultWithTLS12(),
},
}
// uploadPanicLogs attempts to upload all the panic logs in the named // uploadPanicLogs attempts to upload all the panic logs in the named
// directory to the crash reporting server as urlBase. Uploads are attempted // directory to the crash reporting server as urlBase. Uploads are attempted
// with the newest log first. // with the newest log first.
@@ -89,7 +102,7 @@ func uploadPanicLog(ctx context.Context, urlBase, file string) error {
defer headCancel() defer headCancel()
headReq = headReq.WithContext(headCtx) headReq = headReq.WithContext(headCtx)
resp, err := http.DefaultClient.Do(headReq) resp, err := crashReportClient.Do(headReq)
if err != nil { if err != nil {
return err return err
} }
@@ -110,7 +123,7 @@ func uploadPanicLog(ctx context.Context, urlBase, file string) error {
defer putCancel() defer putCancel()
putReq = putReq.WithContext(putCtx) putReq = putReq.WithContext(putCtx)
resp, err = http.DefaultClient.Do(putReq) resp, err = crashReportClient.Do(putReq)
if err != nil { if err != nil {
return err return err
} }
+14 -1
View File
@@ -15,9 +15,11 @@ import (
"time" "time"
"github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/dialer"
"github.com/syncthing/syncthing/lib/osutil" "github.com/syncthing/syncthing/lib/osutil"
"github.com/syncthing/syncthing/lib/rand" "github.com/syncthing/syncthing/lib/rand"
"github.com/syncthing/syncthing/lib/relay/protocol" "github.com/syncthing/syncthing/lib/relay/protocol"
"github.com/syncthing/syncthing/lib/tlsutil"
) )
type dynamicClient struct { type dynamicClient struct {
@@ -31,6 +33,17 @@ type dynamicClient struct {
client *staticClient client *staticClient
} }
// The client used for looking up available relays. Lookups are infrequent,
// so the connection is not kept open afterwards.
var relayPoolClient = &http.Client{
Transport: &http.Transport{
DialContext: dialer.DialContext,
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
TLSClientConfig: tlsutil.SecureDefaultWithTLS12(),
},
}
func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *dynamicClient { func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *dynamicClient {
c := &dynamicClient{ c := &dynamicClient{
pooladdr: uri, pooladdr: uri,
@@ -55,7 +68,7 @@ func (c *dynamicClient) serve(ctx context.Context) error {
return err return err
} }
req.Header.Set("User-Agent", build.UserAgent()) req.Header.Set("User-Agent", build.UserAgent())
data, err := http.DefaultClient.Do(req) data, err := relayPoolClient.Do(req)
if err != nil { if err != nil {
l.Debugln(c, "failed to lookup dynamic relays", err) l.Debugln(c, "failed to lookup dynamic relays", err)
return err return err
+4 -3
View File
@@ -64,9 +64,10 @@ const (
var upgradeClient = &http.Client{ var upgradeClient = &http.Client{
Timeout: readTimeout, Timeout: readTimeout,
Transport: &http.Transport{ Transport: &http.Transport{
DialContext: dialer.DialContext, DialContext: dialer.DialContext,
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsutil.SecureDefaultWithTLS12(), DisableKeepAlives: true, // upgrade checks are hours apart, so don't keep the connection open
TLSClientConfig: tlsutil.SecureDefaultWithTLS12(),
}, },
} }
+4 -3
View File
@@ -200,9 +200,10 @@ func sendFailureReports(ctx context.Context, reports []contract.FailureReport, u
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
DialContext: dialer.DialContext, DialContext: dialer.DialContext,
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsutil.SecureDefaultWithTLS12(), DisableKeepAlives: true, // a new client is used for each send, so don't keep the connection open
TLSClientConfig: tlsutil.SecureDefaultWithTLS12(),
}, },
} }
+3 -2
View File
@@ -359,8 +359,9 @@ func (s *Service) sendUsageReport(ctx context.Context) error {
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
DialContext: dialer.DialContext, DialContext: dialer.DialContext,
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true, // reports are sent once a day, so don't keep the connection open
TLSClientConfig: &tls.Config{ TLSClientConfig: &tls.Config{
InsecureSkipVerify: s.cfg.Options().URPostInsecurely, InsecureSkipVerify: s.cfg.Options().URPostInsecurely,
MinVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS12,