From 33a6db259989673d4bb4ecdd40f9d916c717cd6d Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 29 Aug 2024 22:26:41 +0700 Subject: [PATCH] Configure timeout for HTTP2 transport Otherwise, a stale TCP connection may still alive for too long, causing unexpected failed to connect upstream error when network changed. --- config.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/config.go b/config.go index e61f869..e09fdad 100644 --- a/config.go +++ b/config.go @@ -25,6 +25,7 @@ import ( "github.com/go-playground/validator/v10" "github.com/miekg/dns" "github.com/spf13/viper" + "golang.org/x/net/http2" "golang.org/x/sync/singleflight" "tailscale.com/logtail/backoff" "tailscale.com/net/tsaddr" @@ -489,6 +490,13 @@ func (uc *UpstreamConfig) newDOHTransport(addrs []string) *http.Transport { ClientSessionCache: tls.NewLRUClientSessionCache(0), } + // Prevent bad tcp connection hanging the requests for too long. + // See: https://github.com/golang/go/issues/36026 + if t2, err := http2.ConfigureTransports(transport); err == nil { + t2.ReadIdleTimeout = 10 * time.Second + t2.PingTimeout = 5 * time.Second + } + dialerTimeoutMs := 2000 if uc.Timeout > 0 && uc.Timeout < dialerTimeoutMs { dialerTimeoutMs = uc.Timeout