-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: random port retry logic (#1692)
- Loading branch information
1 parent
5d1476e
commit 419f02e
Showing
5 changed files
with
176 additions
and
24 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
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,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 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 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