diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-10-28 03:22:01 +0300 |
---|---|---|
committer | Jaime Martinez <jmartinez@gitlab.com> | 2020-10-28 03:38:43 +0300 |
commit | 82897d44ba24025be92b99986abafac94770e679 (patch) | |
tree | 548091c7c3746a4a9f5e6b9450ca63a1e0744f7b | |
parent | ea56f68aafc7db0d1280889a485e802a06afddf1 (diff) |
Add const for TTFB timeout
-rw-r--r-- | internal/httprange/http_reader.go | 2 | ||||
-rw-r--r-- | internal/httptransport/transport.go | 11 | ||||
-rw-r--r-- | internal/httptransport/transport_test.go | 3 | ||||
-rw-r--r-- | internal/source/gitlab/client/client.go | 2 |
4 files changed, 11 insertions, 7 deletions
diff --git a/internal/httprange/http_reader.go b/internal/httprange/http_reader.go index 8e632212..467256a0 100644 --- a/internal/httprange/http_reader.go +++ b/internal/httprange/http_reader.go @@ -62,7 +62,7 @@ var httpClient = &http.Client{ metrics.HTTPRangeTraceDuration, metrics.HTTPRangeRequestDuration, metrics.HTTPRangeRequestsTotal, - 15*time.Second, + httptransport.DefaultTTFBTimeout, ), } diff --git a/internal/httptransport/transport.go b/internal/httptransport/transport.go index fd5473c8..d8e6a3fe 100644 --- a/internal/httptransport/transport.go +++ b/internal/httptransport/transport.go @@ -16,6 +16,13 @@ import ( log "github.com/sirupsen/logrus" ) +const ( + // DefaultTTFBTimeout is the timeout used in the meteredRoundTripper + // when calling http.Transport.RoundTrip. The request will be cancelled + // if the response takes longer than this. + DefaultTTFBTimeout = 15 * time.Second +) + var ( sysPoolOnce = &sync.Once{} sysPool *x509.CertPool @@ -98,9 +105,7 @@ func (mrt *meteredRoundTripper) RoundTrip(r *http.Request) (*http.Response, erro ctx := httptrace.WithClientTrace(r.Context(), mrt.newTracer(start)) ctx, cancel := context.WithCancel(ctx) - timer := time.AfterFunc(mrt.ttfbTimeout, func() { - cancel() - }) + timer := time.AfterFunc(mrt.ttfbTimeout, cancel) defer timer.Stop() r = r.WithContext(ctx) diff --git a/internal/httptransport/transport_test.go b/internal/httptransport/transport_test.go index 869f21db..47167a75 100644 --- a/internal/httptransport/transport_test.go +++ b/internal/httptransport/transport_test.go @@ -115,11 +115,10 @@ type mockRoundTripper struct { } func (mrt *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { - time.Sleep(mrt.timeout) select { case <-r.Context().Done(): return nil, r.Context().Err() - default: + case <-time.After(mrt.timeout): return mrt.res, mrt.err } } diff --git a/internal/source/gitlab/client/client.go b/internal/source/gitlab/client/client.go index 2b80e832..b11ea2cb 100644 --- a/internal/source/gitlab/client/client.go +++ b/internal/source/gitlab/client/client.go @@ -61,7 +61,7 @@ func NewClient(baseURL string, secretKey []byte, connectionTimeout, jwtTokenExpi metrics.DomainsSourceAPITraceDuration, metrics.DomainsSourceAPICallDuration, metrics.DomainsSourceAPIReqTotal, - 15*time.Second, + httptransport.DefaultTTFBTimeout, ), }, jwtTokenExpiry: jwtTokenExpiry, |