node-modules / agentkeepalive

Support keepalive http agent.
MIT License
579 stars 57 forks source link

How to determine/debug if connections are being reused? #95

Open Ghazgkull opened 3 years ago

Ghazgkull commented 3 years ago

I'm attempting to enable connection reuse for a simple application using this module, but I'm not sure how to verify whether connections are being reused.

I tried adding some simple logging after each request to look at the agent status. Is that the right way to determine if it's working?

Using the default options, I tried making three requests using an HTTPS agent, and I see the following statuses printed using JSON.stringify(agent.getCurrentStatus()). Am I right to think that connections aren't being reused here? Is there any way to debug connection reuse?


Agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":0,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":1,"freeSockets":{"redacted.com:443::::::::::::::::::":1},"sockets":{},"requests":{}}
Agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":0,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":1,"freeSockets":{"redacted.com:443::::::::::::::::::":1},"sockets":{},"requests":{}}
Agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":0,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":1,"freeSockets":{"redacted.com:443::::::::::::::::::":1},"sockets":{},"requests":{}}```
Ghazgkull commented 3 years ago

After further exploration, I think the problem was that I was only using the agent to make serial requests. By swapping my agent scope to reuse the same agent for a large number of parallel requests, the status data started to really make sense and clearly showed connection reuse.

For example:


  agent status: {"createSocketCount":2007,"createSocketErrorCount":0,"closeSocketCount":1382,"errorSocketCount":1,"timeoutSocketCount":1,"requestCount":92467,"freeSockets":{"redacted.com:443::::::::::::::::::":107},"sockets":{"redacted.com:443::::::::::::::::::":518},"requests":{}}```