tyk
tyk copied to clipboard
[TT-5483] batch requests: http transport/tcp connections leak when `TykBatchRequest` called in jsvm
Branch/Environment/Version
- Branch/Version: master or v3.2.1
Describe the bug
When TykBatchRequest
called to do lots of http requests in jsvm scripts, the http underlay tcp connection will leak (keeps alive at all time).
Reproduction steps Steps to reproduce the behavior:
- call
TykBatchRequest
many times or do lots of http request in js scripts - run
netstat -pant
in the tyk host/pod
Actual behavior The tcp connections keeps there.
Expected behavior The tcp connections were released.
Screenshots/Video
Logs (debug mode or log file):
Log from console or from log file.
Configuration (tyk config file): Attach tyk configuration file
Additional context
TykMakeHttpRequest
works well because it add Connection: close
in http request header.
https://github.com/TykTechnologies/tyk/blob/272b8ba0fcb3148ddcff8ac996ffd402d6e53933/gateway/mw_js_plugin.go#L518
One simple fixing idea is that:
we add a request.Close = true
before setting custom headers, this can make the batch requests http transport connection closed by default.
https://github.com/TykTechnologies/tyk/blob/272b8ba0fcb3148ddcff8ac996ffd402d6e53933/gateway/batch_requests.go#L114
Or we can make a global single http.Transport{} (or http.Client{}) in jsvm midware and set MaxIdleConnsPerHost
or MaxIdleConns
larger , then the http requests in jsvm will share/reuse the tcp connections owned by http.Transport{}.