-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Random port retry logic #1692
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
Merged
Merged
Random port retry logic #1692
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c50591e
feat(na): implement random port retry logic.
u9520107 6dc10f0
refact(na): portfinding to utils.
u9520107 be2ae3e
test(na): add tests for findPort cli util.
u9520107 ab7e79d
docs(na): update comment from 10 to 3 times.
mistic 14ab660
extract duplicate server.listen into a local function
u9520107 0413201
add comment about tryParseInt helper function
u9520107 73a385b
refact(na): make a more readable find port function.
mistic 8604b28
refact(na): move findPort and tryParseInt from utils file to separate…
mistic 6727de2
Merge branch 'master' into port-retry-logic
evilebottnawi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,35 @@ | ||
'use strict'; | ||
|
||
const portfinder = require('portfinder'); | ||
|
||
function runPortFinder(defaultPort, cb) { | ||
portfinder.basePort = defaultPort; | ||
portfinder.getPort((err, port) => { | ||
cb(err, port); | ||
}); | ||
} | ||
|
||
function findPort(server, defaultPort, defaultPortRetry, fn) { | ||
let tryCount = 0; | ||
const portFinderRunCb = (err, port) => { | ||
tryCount += 1; | ||
fn(err, port); | ||
}; | ||
|
||
server.listeningApp.on('error', (err) => { | ||
if (err && err.code !== 'EADDRINUSE') { | ||
throw err; | ||
} | ||
|
||
if (tryCount >= defaultPortRetry) { | ||
fn(err); | ||
return; | ||
} | ||
|
||
runPortFinder(defaultPort, portFinderRunCb); | ||
}); | ||
|
||
runPortFinder(defaultPort, portFinderRunCb); | ||
} | ||
|
||
module.exports = findPort; |
This file contains hidden or 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,11 @@ | ||
'use strict'; | ||
|
||
function tryParseInt(input) { | ||
const output = parseInt(input, 10); | ||
if (Number.isNaN(output)) { | ||
return null; | ||
} | ||
return output; | ||
} | ||
|
||
module.exports = tryParseInt; |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
IMO: I think we should leave
process.env.DEFAULT_PORT_RETRY
as a document.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.
@hiroppy can you create issue in webpack docs repo?
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.
I think
DEFAULT_PORT_RETRY
should be added indevServer
because currently, we don' useprocess.env
. see https://webpack.js.org/configuration/dev-server/@evilebottnawi what do you think?
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.
@hiroppy yep, after merge we need open issue here https://github.com/webpack/webpack.js.org 👍