Skip to content

Commit

Permalink
Fix max_conn_time
Browse files Browse the repository at this point in the history
`HTTPTransportCreated` was never set
  • Loading branch information
buger committed Mar 7, 2018
1 parent 4a7e0ac commit 57b555e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,4 +1150,29 @@ func TestKeepAliveConns(t *testing.T) {
{Code: 200},
}...)
})

t.Run("Should respect max_conn_time", func(t *testing.T) {
config.Global.CloseConnections = false
// Allow 2 connection with 2 reads
upstream := createTestUptream(t, 2, 2)
defer upstream.Close()
config.Global.MaxConnTime = 1

spec := buildAndLoadAPI(func(spec *APISpec) {
spec.Proxy.ListenPath = "/"
spec.Proxy.TargetURL = "http://" + upstream.Addr().String()
})[0]

ts.Run(t, []test.TestCase{
{Code: 200},
{Code: 200},
}...)

// Set in past to re-create transport
spec.HTTPTransportCreated = time.Now().Add(-time.Minute)

// Should be called in new connection
// We already made 2 requests above, so 3th in same not allowed
ts.Run(t, test.TestCase{Code: 200})
})
}
2 changes: 2 additions & 0 deletions reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,15 @@ func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Reques
p.TykAPISpec.Lock()

createTransport := p.TykAPISpec.HTTPTransport == nil

if !createTransport && config.Global.MaxConnTime != 0 {
createTransport = time.Since(p.TykAPISpec.HTTPTransportCreated) > time.Duration(config.Global.MaxConnTime)*time.Second
}

if createTransport {
_, timeout := p.CheckHardTimeoutEnforced(p.TykAPISpec, req)
p.TykAPISpec.HTTPTransport = httpTransport(timeout, rw, req, p)
p.TykAPISpec.HTTPTransportCreated = time.Now()
} else if IsWebsocket(req) { // check if it is an upgrade request to NEW WS-connection
// overwrite transport's ResponseWriter from previous upgrade request
// as it was already hijacked and now is being used for other connection
Expand Down

0 comments on commit 57b555e

Please sign in to comment.