Skip to content

Commit

Permalink
HTTPSamplingStrategyFetcher: Use http client with 10 second timeout (j…
Browse files Browse the repository at this point in the history
…aegertracing#578)

* Use http client with 10 second timeout

Signed-off-by: Joe Elliott <[email protected]>

* lint

Signed-off-by: Joe Elliott <[email protected]>
  • Loading branch information
joe-elliott authored Apr 28, 2021
1 parent ba87d35 commit 7a33f7c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
22 changes: 18 additions & 4 deletions sampler_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
)

const (
defaultRemoteSamplingTimeout = 10 * time.Second
defaultSamplingRefreshInterval = time.Minute
)

Expand Down Expand Up @@ -298,17 +299,30 @@ 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) {
v := url.Values{}
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
}
Expand Down
5 changes: 1 addition & 4 deletions sampler_remote_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7a33f7c

Please sign in to comment.