tyk icon indicating copy to clipboard operation
tyk copied to clipboard

[TT-5483] batch requests: http transport/tcp connections leak when `TykBatchRequest` called in jsvm

Open lining2020x opened this issue 2 years ago • 1 comments

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:

  1. call TykBatchRequest many times or do lots of http request in js scripts
  2. run netstat -pant in the tyk host/pod

Actual behavior The tcp connections keeps there.

Expected behavior The tcp connections were released.

Screenshots/Video image

Logs (debug mode or log file): Log from console or from log file. image

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

lining2020x avatar May 17 '22 10:05 lining2020x

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{}.

lining2020x avatar May 17 '22 10:05 lining2020x