Skip to content

Commit

Permalink
Make HTTP transport API spec property
Browse files Browse the repository at this point in the history
Right now Transport is created for each requests, which means that
connection pooling is not used and on each request it creates new
connection to the upstream server.

This is differ from 2.3 behavior, which had global Transport for all
APIs. During the 2.4 refactor it was changed to behavior described
above.

This change introduce transport caching, so it gets re-used between API
calls. In addition, this is better than 2.3 approach, because each API
can have different transport settings and timeouts, and now each API
gets own transport.
  • Loading branch information
buger committed Nov 3, 2017
1 parent bdae5a4 commit 1f38dc8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ type APISpec struct {
LastGoodHostList *apidef.HostList
HasRun bool
ServiceRefreshInProgress bool
HTTPTransport http.RoundTripper
}

// APIDefinitionLoader will load an Api definition from a storage
Expand Down
10 changes: 6 additions & 4 deletions reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,10 @@ func httpTransport(timeOut int, rw http.ResponseWriter, req *http.Request, p *Re

func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Request, withCache bool) *http.Response {
// 1. Check if timeouts are set for this endpoint
_, timeout := p.CheckHardTimeoutEnforced(p.TykAPISpec, req)
transport := httpTransport(timeout, rw, req, p)
if p.TykAPISpec.HTTPTransport == nil {
_, timeout := p.CheckHardTimeoutEnforced(p.TykAPISpec, req)
p.TykAPISpec.HTTPTransport = httpTransport(timeout, rw, req, p)
}

ctx := req.Context()
if cn, ok := rw.(http.CloseNotifier); ok {
Expand Down Expand Up @@ -525,14 +527,14 @@ func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Reques
p.ErrorHandler.HandleError(rw, logreq, "Service temporarily unnavailable.", 503)
return nil
}
res, err = transport.RoundTrip(outreq)
res, err = p.TykAPISpec.HTTPTransport.RoundTrip(outreq)
if err != nil || res.StatusCode == 500 {
breakerConf.CB.Fail()
} else {
breakerConf.CB.Success()
}
} else {
res, err = transport.RoundTrip(outreq)
res, err = p.TykAPISpec.HTTPTransport.RoundTrip(outreq)
}

if err != nil {
Expand Down

0 comments on commit 1f38dc8

Please sign in to comment.