-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
net: support AbortSignal in server.listen #36623
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ const eos = require('internal/streams/end-of-stream'); | |
const { ERR_INVALID_ARG_TYPE } = codes; | ||
|
||
// This method is inlined here for readable-stream | ||
// It also does not allow for signal to not exist on the steam | ||
// It also does not allow for signal to not exist on the stream | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated change, but good catch ;) it might be worth having it land in a separate PR (in case this one was reverted or didn't get backported to LTS release lines). |
||
// https://github.com/nodejs/node/pull/36061#discussion_r533718029 | ||
const validateAbortSignal = (signal, name) => { | ||
if (typeof signal !== 'object' || | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,7 @@ const { | |
} = require('internal/errors'); | ||
const { isUint8Array } = require('internal/util/types'); | ||
const { | ||
validateAbortSignal, | ||
validateInt32, | ||
validatePort, | ||
validateString | ||
|
@@ -1148,6 +1149,22 @@ function afterConnect(status, handle, req, readable, writable) { | |
} | ||
} | ||
|
||
function addAbortSignalOption(self, options) { | ||
if (options?.signal === undefined) { | ||
return; | ||
} | ||
validateAbortSignal(options.signal, 'options.signal'); | ||
const { signal } = options; | ||
const onAborted = () => { | ||
self.close(); | ||
}; | ||
if (signal.aborted) { | ||
process.nextTick(onAborted); | ||
} else { | ||
signal.addEventListener('abort', onAborted); | ||
self.once('close', () => signal.removeEventListener('abort', onAborted)); | ||
} | ||
} | ||
|
||
function Server(options, connectionListener) { | ||
if (!(this instanceof Server)) | ||
|
@@ -1398,6 +1415,7 @@ Server.prototype.listen = function(...args) { | |
listenInCluster(this, null, -1, -1, backlogFromArgs); | ||
return this; | ||
} | ||
addAbortSignalOption(this, options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this function is only used once, is it relevant to have it as a separate function instead of inlining it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just thought that it would make the |
||
// (handle[, backlog][, cb]) where handle is an object with a fd | ||
if (typeof options.fd === 'number' && options.fd >= 0) { | ||
listenInCluster(this, null, null, null, backlogFromArgs, options.fd); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
|
||
{ | ||
// Test bad signal. | ||
const server = net.createServer(); | ||
assert.throws( | ||
() => server.listen({ port: 0, signal: 'INVALID_SIGNAL' }), | ||
{ | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
name: 'TypeError' | ||
}); | ||
} | ||
|
||
{ | ||
// Test close. | ||
const server = net.createServer(); | ||
const controller = new AbortController(); | ||
server.on('close', common.mustCall()); | ||
server.listen({ port: 0, signal: controller.signal }); | ||
controller.abort(); | ||
} | ||
|
||
{ | ||
// Test close with pre-aborted signal. | ||
const server = net.createServer(); | ||
const controller = new AbortController(); | ||
controller.abort(); | ||
server.on('close', common.mustCall()); | ||
server.listen({ port: 0, signal: controller.signal }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a docs example would be nice - namely mentioning this calls
.close
internally and doesn't stop ongoing connections the server is already serving.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added