forked from smithy-lang/smithy-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node-http-handler): defer socket event listener attachment (smit…
- Loading branch information
Showing
7 changed files
with
128 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@smithy/node-http-handler": minor | ||
--- | ||
|
||
defer socket event listeners for node:http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,43 @@ | ||
import { ClientRequest } from "http"; | ||
import { Socket } from "net"; | ||
|
||
export const setConnectionTimeout = (request: ClientRequest, reject: (err: Error) => void, timeoutInMs = 0) => { | ||
const DEFER_EVENT_LISTENER_TIME = 1000; | ||
|
||
export const setConnectionTimeout = ( | ||
request: ClientRequest, | ||
reject: (err: Error) => void, | ||
timeoutInMs = 0 | ||
): NodeJS.Timeout | number => { | ||
if (!timeoutInMs) { | ||
return; | ||
return -1; | ||
} | ||
|
||
// Throw a connecting timeout error unless a connection is made within time. | ||
const timeoutId = setTimeout(() => { | ||
request.destroy(); | ||
reject( | ||
Object.assign(new Error(`Socket timed out without establishing a connection within ${timeoutInMs} ms`), { | ||
name: "TimeoutError", | ||
}) | ||
); | ||
}, timeoutInMs); | ||
const registerTimeout = (offset: number) => { | ||
// Throw a connecting timeout error unless a connection is made within time. | ||
const timeoutId = setTimeout(() => { | ||
request.destroy(); | ||
reject( | ||
Object.assign(new Error(`Socket timed out without establishing a connection within ${timeoutInMs} ms`), { | ||
name: "TimeoutError", | ||
}) | ||
); | ||
}, timeoutInMs - offset); | ||
|
||
request.on("socket", (socket: Socket) => { | ||
if (socket.connecting) { | ||
socket.on("connect", () => { | ||
request.on("socket", (socket: Socket) => { | ||
if (socket.connecting) { | ||
socket.on("connect", () => { | ||
clearTimeout(timeoutId); | ||
}); | ||
} else { | ||
clearTimeout(timeoutId); | ||
}); | ||
} else { | ||
clearTimeout(timeoutId); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
if (timeoutInMs < 2000) { | ||
registerTimeout(0); | ||
return 0; | ||
} | ||
|
||
return setTimeout(registerTimeout.bind(null, DEFER_EVENT_LISTENER_TIME), DEFER_EVENT_LISTENER_TIME); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,31 @@ | ||
import { ClientRequest } from "http"; | ||
|
||
const DEFER_EVENT_LISTENER_TIME = 3000; | ||
|
||
export interface SocketKeepAliveOptions { | ||
keepAlive: boolean; | ||
keepAliveMsecs?: number; | ||
} | ||
|
||
export const setSocketKeepAlive = (request: ClientRequest, { keepAlive, keepAliveMsecs }: SocketKeepAliveOptions) => { | ||
export const setSocketKeepAlive = ( | ||
request: ClientRequest, | ||
{ keepAlive, keepAliveMsecs }: SocketKeepAliveOptions, | ||
deferTimeMs = DEFER_EVENT_LISTENER_TIME | ||
): NodeJS.Timeout | number => { | ||
if (keepAlive !== true) { | ||
return; | ||
return -1; | ||
} | ||
|
||
const registerListener = () => { | ||
request.on("socket", (socket) => { | ||
socket.setKeepAlive(keepAlive, keepAliveMsecs || 0); | ||
}); | ||
}; | ||
|
||
if (deferTimeMs === 0) { | ||
registerListener(); | ||
return 0; | ||
} | ||
|
||
request.on("socket", (socket) => { | ||
socket.setKeepAlive(keepAlive, keepAliveMsecs || 0); | ||
}); | ||
return setTimeout(registerListener, deferTimeMs); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
import { ClientRequest } from "http"; | ||
|
||
export const setSocketTimeout = (request: ClientRequest, reject: (err: Error) => void, timeoutInMs = 0) => { | ||
request.setTimeout(timeoutInMs, () => { | ||
// destroy the request | ||
request.destroy(); | ||
reject(Object.assign(new Error(`Connection timed out after ${timeoutInMs} ms`), { name: "TimeoutError" })); | ||
}); | ||
const DEFER_EVENT_LISTENER_TIME = 3000; | ||
|
||
export const setSocketTimeout = ( | ||
request: ClientRequest, | ||
reject: (err: Error) => void, | ||
timeoutInMs = 0 | ||
): NodeJS.Timeout | number => { | ||
const registerTimeout = (offset: number) => { | ||
request.setTimeout(timeoutInMs - offset, () => { | ||
// destroy the request | ||
request.destroy(); | ||
reject(Object.assign(new Error(`Connection timed out after ${timeoutInMs} ms`), { name: "TimeoutError" })); | ||
}); | ||
}; | ||
|
||
if (0 < timeoutInMs && timeoutInMs < 6000) { | ||
registerTimeout(0); | ||
return 0; | ||
} | ||
|
||
return setTimeout( | ||
registerTimeout.bind(null, timeoutInMs === 0 ? 0 : DEFER_EVENT_LISTENER_TIME), | ||
DEFER_EVENT_LISTENER_TIME | ||
); | ||
}; |