Skip to content
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

Random port retry logic #1692

Merged
merged 9 commits into from
Feb 26, 2019
Merged

Conversation

mistic
Copy link
Contributor

@mistic mistic commented Feb 25, 2019

  • This is a bugfix
  • This is a feature
  • This is a code refactor
  • This is a test update
  • This is a docs update
  • This is a metadata update

For Bugs and Features; did you add new tests?

Some find port tests were added.

Motivation / Use-Case

Closes #1572.
The main change is to delay port finding to just before trying to run server.listen. Just this change alone reduced the time window between determining the available port and actually listening to it, as previously the compiler is initiated between this time frame. This naturally let most instances attempt to get available port in different moments and reducing the risk of trying to use the same port.
Another change is to listen to error event when port finding is used, when 'EADDRINUSE' error is encountered, retry the port finding again for up to 10 times. Not the most elegant solution, but should be sufficient for most prototyping scenarios.
If retried for 10 times and still cannot find available pot to use, the dev-server will error out like before.

Breaking Changes

Unless there are people relying on the fact that attempting to start multiple instances without specifying port could result in errors, this should not have breaking changes.

Additional Info

This is recreation of #1577 which seems to have any activity. As this is an important feature, this PR presents the original work with some refactoring and also conflict free state after merged against last master. The original commits were cherry picked and most of the credits should be given to the original author of the PR.

A manual test of the feature was also performed.

/CC @evilebottnawi

@jsf-clabot

This comment has been minimized.

@codecov
Copy link

codecov bot commented Feb 26, 2019

Codecov Report

Merging #1692 into master will decrease coverage by 0.62%.
The diff coverage is 94.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1692      +/-   ##
==========================================
- Coverage   84.39%   83.76%   -0.63%     
==========================================
  Files           7        8       +1     
  Lines         519      536      +17     
  Branches      159      161       +2     
==========================================
+ Hits          438      449      +11     
- Misses         64       70       +6     
  Partials       17       17
Impacted Files Coverage Δ
lib/utils/findPort.js 94.11% <94.11%> (ø)
lib/utils/createCertificate.js 66.66% <0%> (-33.34%) ⬇️
lib/Server.js 78.89% <0%> (-1.06%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5d1476e...6727de2. Read the comment docs.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

@alexander-akait
Copy link
Member

/cc @hiroppy

// Because NaN == null is false, defaultTo fails if parseInt returns NaN
// so the tryParseInt function is introduced to handle NaN
const defaultPortRetry = defaultTo(
tryParseInt(process.env.DEFAULT_PORT_RETRY),
Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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 in devServer because currently, we don' use process.env. see https://webpack.js.org/configuration/dev-server/

@evilebottnawi what do you think?

Copy link
Member

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 👍

Copy link
Member

@hiroppy hiroppy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM:)

@alexander-akait alexander-akait merged commit 419f02e into webpack:master Feb 26, 2019
@u9520107
Copy link

Thanks for doing this. I must apologize for putting this aside for too long. Got bombarded with work right after our Chinese New Year holidays, and I totally forgot to put this into my reminders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Retry port finding
6 participants