Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: http keep alive now works #20

Merged
merged 1 commit into from
Nov 10, 2024

Conversation

Mall0c
Copy link

@Mall0c Mall0c commented Oct 30, 2024

Thank you for this simple yet great library.

I am using this library for debugging purposes. I have added a custom HTTP Agent to superagent, because I'd like to use HTTP Keep-Alive. I am issuing many requests per second to the same server and establishing a new TCP connection every time is a waste of ressources.

The HTTP agent is created as simple as this example:

new http.Agent({
    keepAlive: true
})

and then provided to superagent with the .agent() method:

superagent
	.get("foobar")
    .use(logNetworkTime((err, result) => {
        if (err) {
            console.log(err)
        } else {
            console.log(timing)
        }
    }))
    .send("json obj")
    .timeout(30000)
    .set("Content-Type", "application/json")
    .set("Accept", "application/json")
    .auth("user", "pass")
    .agent(globalThis.HttpAgent)

This would give me the following exception:

/path/to/app/node_modules/superagent-node-http-timings/index.js:78
  const secondDiff = endTime[0] - startTime[0];
                                           ^
TypeError: Cannot read properties of undefined (reading '0')
    at getHrTimeDurationInMs (/path/to/app/node_modules/superagent-node-http-timings/index.js:78:44)
    at getTimings (/path/to/app/node_modules/superagent-node-http-timings/index.js:68:16)
    at IncomingMessage.<anonymous> (/path/to/app/node_modules/superagent-node-http-timings/index.js:44:22)
    at Stream.emit (node:events:531:35)
    at Unzip.<anonymous> (/path/to/app/node_modules/superagent/src/node/unzip.js:55:12)
    at Unzip.emit (node:events:519:28)
    at endReadableNT (node:internal/streams/readable:1696:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

After some debugging I found out its due to the HTTP Keep Alive flag. The agent maintains a pool of reusable TCP connections to the server I'm talking to. That means, there is no tcpConnectionAt variable, since there is no connect event on subsequent HTTP requests.

I have provided a fix.

Copy link
Owner

@webuniverseio webuniverseio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@webuniverseio webuniverseio merged commit a0d9d36 into webuniverseio:master Nov 10, 2024
@webuniverseio
Copy link
Owner

I'll look into updating npm package version next Monday.

webuniverseio added a commit that referenced this pull request Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants