From 7a33f7c403fe2ba1e2d25db2b0a318d038a861d4 Mon Sep 17 00:00:00 2001 From: Joe Elliott Date: Wed, 28 Apr 2021 14:34:56 -0400 Subject: [PATCH] HTTPSamplingStrategyFetcher: Use http client with 10 second timeout (#578) * Use http client with 10 second timeout Signed-off-by: Joe Elliott * lint Signed-off-by: Joe Elliott --- sampler_remote.go | 22 ++++++++++++++++++---- sampler_remote_options.go | 5 +---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/sampler_remote.go b/sampler_remote.go index 0b3fbbe7..119f0a1b 100644 --- a/sampler_remote.go +++ b/sampler_remote.go @@ -29,6 +29,7 @@ import ( ) const ( + defaultRemoteSamplingTimeout = 10 * time.Second defaultSamplingRefreshInterval = time.Minute ) @@ -298,8 +299,22 @@ func (u *AdaptiveSamplerUpdater) Update(sampler SamplerV2, strategy interface{}) // ----------------------- type httpSamplingStrategyFetcher struct { - serverURL string - logger log.DebugLogger + serverURL string + logger log.DebugLogger + httpClient http.Client +} + +func newHTTPSamplingStrategyFetcher(serverURL string, logger log.DebugLogger) *httpSamplingStrategyFetcher { + customTransport := http.DefaultTransport.(*http.Transport).Clone() + customTransport.ResponseHeaderTimeout = defaultRemoteSamplingTimeout + + return &httpSamplingStrategyFetcher{ + serverURL: serverURL, + logger: logger, + httpClient: http.Client{ + Transport: customTransport, + }, + } } func (f *httpSamplingStrategyFetcher) Fetch(serviceName string) ([]byte, error) { @@ -307,8 +322,7 @@ func (f *httpSamplingStrategyFetcher) Fetch(serviceName string) ([]byte, error) v.Set("service", serviceName) uri := f.serverURL + "?" + v.Encode() - // TODO create and reuse http.Client with proper timeout settings, etc. - resp, err := http.Get(uri) + resp, err := f.httpClient.Get(uri) if err != nil { return nil, err } diff --git a/sampler_remote_options.go b/sampler_remote_options.go index e4a6108b..64b028bf 100644 --- a/sampler_remote_options.go +++ b/sampler_remote_options.go @@ -140,10 +140,7 @@ func (o *samplerOptions) applyOptionsAndDefaults(opts ...SamplerOption) *sampler o.samplingRefreshInterval = defaultSamplingRefreshInterval } if o.samplingFetcher == nil { - o.samplingFetcher = &httpSamplingStrategyFetcher{ - serverURL: o.samplingServerURL, - logger: o.logger, - } + o.samplingFetcher = newHTTPSamplingStrategyFetcher(o.samplingServerURL, o.logger) } if o.samplingParser == nil { o.samplingParser = new(samplingStrategyParser)