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(node-http-handler): call socket.setKeepAlive if enabled in http(s)Agent #4561

Merged
merged 19 commits into from
May 4, 2023

Conversation

yenfryherrerafeliz
Copy link
Contributor

@yenfryherrerafeliz yenfryherrerafeliz commented Mar 21, 2023

When using node-http-handler and setting keep-alive to true in the agent configuration, this setup is not being honored by nodejs https library, due to a bug with the https lib from nodejs. To aid this issue at the SDK side what I am doing is enabling keep alive directly in the socket, by adding a listener for when a new socket is created, and there we provide the keep-alive settings to the socket.

Issue

Fixes #3624

Description

what I do is to add a listener for when a new socket is created then we pass the keep alive settings to that socket.
Example:

import { NodeHttpHandler }  from '@aws-sdk/node-http-handler';
import {InvokeCommand, LambdaClient} from "@aws-sdk/client-lambda";

const client = new LambdaClient({
    region: 'us-east-2',
    requestHandler: new NodeHttpHandler({
        httpsAgent: new Agent({
            keepAlive: true,
            keepAliveMsecs: 5 * 1000
        })
    })
})

And the configuration above will make the socket to send keep-alive packets every 5 seconds.

Testing

N/A

Additional context

I have also created the following issue under nodejs repo.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

When using node-http-handler and setting keep-alive to true in the agent configuration, this setup is not being honored by nodejs https library due to a bug at their side. To aid this issue at the SDK side what I am doing is enabling keep alive directly in the socket, by adding a listener for when a new socket is created, and there we provide the keep-alive settings to the socket. I add this configuration as option so that we need to explicity say we want to enable keep-alive in the socket to keep forward compatibility.
@yenfryherrerafeliz yenfryherrerafeliz requested review from a team as code owners March 21, 2023 20:14
@trivikr trivikr changed the title fix: enable keep-alive in node-http-handler fix(node-http-handler): add socketKeepAlive option Mar 21, 2023
Copy link
Contributor

@kuhe kuhe left a comment

Choose a reason for hiding this comment

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

looks good, just need a unit test and some minor changes

Run prettier formatting over the code to make it looks better.
Change the name of initialDelay to initialDelayInMSecs to better describe how the value needs to be given.
Make initialDelayInMSecs to be optional so that if it is not provided as parameter then it fallbacks into the default value.
Unit tests check the following:
- that when keepAlive is provided as true it should set keepAlive to true to the socket.
- that when keepAlive is provided as true and a custom initial delay is given they are set correctly.
- that when keepAlive is provided as set or not provided at all the keepAlive option is not set to the socket.
Make new changes align with prettier format.
Changed:
import {setSocketKeepAlive, SocketKeepAliveOptions} from "./set-socket-keep-alive";
To:
import {
  setSocketKeepAlive,
  SocketKeepAliveOptions,
} from "./set-socket-keep-alive";
@alfaproject
Copy link

Is this still a issue? Should we add this work-around in our clients until this is merged, or?

Refactor the implementation for enabling keepalive in the socket of the request by considering the options passed to the agent provided to the handler.
Ran prettier over the changed files. Updated the logic that decides when we should set keepalive to sockets, in more accurate way.
@yenfryherrerafeliz yenfryherrerafeliz requested review from trivikr and kuhe May 4, 2023 14:01
@trivikr
Copy link
Member

trivikr commented May 4, 2023

Verified that keepAlive parameter is set on agent

import http from "http";

// Logs false in Node.js 14+
console.log("keepAlive from default agent:", new http.Agent().keepAlive);

// Logs false in Node.js 14+
console.log(
  "keepAlive from agent with keepAlive=false:",
  new http.Agent({ keepAlive: false }).keepAlive
);

// Logs true in Node.js 14+
console.log(
  "keepAlive from agent with keepAlive=true:",
  new http.Agent({ keepAlive: true }).keepAlive
);

@trivikr trivikr changed the title fix(node-http-handler): add socketKeepAlive option fix(node-http-handler): call socket.setKeepAlive if keepAlive is enabled in http(s)Agent May 4, 2023
@trivikr trivikr changed the title fix(node-http-handler): call socket.setKeepAlive if keepAlive is enabled in http(s)Agent fix(node-http-handler): call socket.setKeepAlive if enabled in http(s)Agent May 4, 2023
@trivikr trivikr merged commit bd16ace into aws:main May 4, 2023
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TCP Keepalive probes are not sent when using AWS JS SDK
5 participants