-
Notifications
You must be signed in to change notification settings - Fork 25
fix: try to use next available port #16
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
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 |
---|---|---|
|
@@ -8,26 +8,40 @@ module.exports = (port, callback) => { | |
callback = port; | ||
port = null; | ||
} | ||
port = parseInt(port) || 0; | ||
let maxPort = port + 10; | ||
if (maxPort > 65535) { | ||
maxPort = 65535; | ||
} | ||
debug('detect free port between [%s, %s)', port, maxPort); | ||
if (typeof callback === 'function') { | ||
return tryListen(port, callback); | ||
return tryListen(port, maxPort, callback); | ||
} | ||
// promise | ||
return new Promise(resolve => { | ||
tryListen(port, (_, realPort) => { | ||
tryListen(port, maxPort, (_, realPort) => { | ||
resolve(realPort); | ||
}); | ||
}); | ||
}; | ||
|
||
function tryListen(port, callback) { | ||
port = parseInt(port) || 0; | ||
function tryListen(port, maxPort, callback) { | ||
const server = new net.Server(); | ||
|
||
server.on('error', err => { | ||
debug('listen %s error: %s', port, err); | ||
port = 0; | ||
if (port === 0) { | ||
return callback(err); | ||
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. Why not try another random port? 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. random port won't error on normal case. If listen random port error happen, it's hard to try again, maybe "open file handles" is full on that time. 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. This line is not covered 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. It's hard to mock this error, ignore it for now. |
||
} | ||
|
||
port++; | ||
if (port >= maxPort) { | ||
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort); | ||
port = 0; | ||
maxPort = 0; | ||
} | ||
server.close(); | ||
return tryListen(port, callback); | ||
return tryListen(port, maxPort, callback); | ||
}); | ||
|
||
server.listen({ port }, () => { | ||
|
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.
parseInt(port, 10)
?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.
default radix is 10, no need to set https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt