HTTPPoolOptions.Transport is now used instead of DefaultTransport when provided

This commit is contained in:
Derrick J. Wippler 2019-06-04 09:43:52 -05:00
parent 554745c665
commit 2869a0ce28
2 changed files with 13 additions and 10 deletions

View File

@ -4,18 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [2.0.0-rc.2] - 2019-06-03 ## [2.0.0-rc.3] - 2019-06-03
### Changes ### Changes
* Now using golang standard `context.Context` instead of `groupcache.Context`. * Now using golang standard `context.Context` instead of `groupcache.Context`.
* HTTP requests made by `httpGetter` now respect `context.Context` done. * HTTP requests made by `httpGetter` now respect `context.Context` done.
* Moved `HTTPPool` config `Context` and `Transport` to `HTTPPoolOptions` for consist configuration. * Moved `HTTPPool` config `Context` and `Transport` to `HTTPPoolOptions` for consist configuration.
* Now Associating the transport with peer `httpGetter` so we take advantage of * Now Associating the transport with peer `httpGetter` so there is no need to
connection reuse. This lowers the impact on DNS and improves performance for call `Transport` function for each request.
high request volume low latency applications.
* Now always populating the hotcache. A more complex algorithm is unnecessary * Now always populating the hotcache. A more complex algorithm is unnecessary
when the LRU cache will ensure the most used values remain in the cache. The when the LRU cache will ensure the most used values remain in the cache. The
evict code ensures the hotcache does not overcrowd the maincache. evict code ensures the hotcache does not overcrowd the maincache.
* Changed import paths to /v2 in accordance with go modules rules * Changed import paths to /v2 in accordance with go modules rules
* Fixed Issue where `DefaultTransport` was always used even if `Transport` was
specified by the user.
## [1.3.0] - 2019-05-23 ## [1.3.0] - 2019-05-23
### Added ### Added

14
http.go
View File

@ -124,7 +124,10 @@ func (p *HTTPPool) Set(peers ...string) {
p.peers.Add(peers...) p.peers.Add(peers...)
p.httpGetters = make(map[string]*httpGetter, len(peers)) p.httpGetters = make(map[string]*httpGetter, len(peers))
for _, peer := range peers { for _, peer := range peers {
p.httpGetters[peer] = &httpGetter{getTransport: p.opts.Transport, baseURL: peer + p.opts.BasePath} p.httpGetters[peer] = &httpGetter{
getTransport: p.opts.Transport,
baseURL: peer + p.opts.BasePath,
}
} }
} }
@ -242,15 +245,14 @@ func (h *httpGetter) makeRequest(ctx context.Context, method string, in *pb.GetR
// Pass along the context to the RoundTripper // Pass along the context to the RoundTripper
req = req.WithContext(ctx) req = req.WithContext(ctx)
// Associate the transport with this peer so we take advantage of connection reuse. // Associate the transport with this peer so we don't need to
// call getTransport() every time a request is made.
if h.transport == nil { if h.transport == nil {
if h.getTransport != nil { if h.getTransport != nil {
h.transport = h.getTransport(ctx) h.transport = h.getTransport(ctx)
} else {
h.transport = http.DefaultTransport
} }
// Ensure we have a copy of the default transport and not just a reference.
tr := http.DefaultTransport.(*http.Transport)
trCopy := http.Transport(*tr)
h.transport = &trCopy
} }
res, err := h.transport.RoundTrip(req) res, err := h.transport.RoundTrip(req)