-
Notifications
You must be signed in to change notification settings - Fork 445
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
Use next available port when the default port is already used #2682
base: v3.x
Are you sure you want to change the base?
Use next available port when the default port is already used #2682
Conversation
@kshyju I noticed you flagged this for discussion, have you looked into this further? |
.Where(l => l.Port >= startPort) | ||
.Select(l => l.Port)); | ||
|
||
usedPorts.Sort(); |
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.
Why sorting is needed for all used ports, thats an additional complexity of O(NlogN). You are iterating over all the values in usedPorts.
You have two options,
- Used HashSet and iterate over all the values and lookup will be O(N), then you can do startPort++ and keep looking.
- Used TreeSet or SortedSet to store and look for value less than startPort.
} | ||
startPort++; | ||
} | ||
|
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.
What if all ports are exhausted, then should we log the errors?
@@ -33,5 +37,33 @@ public static int GetAvailablePort() | |||
listener.Stop(); | |||
} | |||
} | |||
|
|||
public static int GetNextAvailablePort(int startPort) | |||
{ |
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.
ArgumentExceptin when startPort is either using for system process or upper limit of 65536
When working with multiple function apps locally, every function host apart from the first needs to have a different port manually set either via the command line argument
--port
or theHost.LocalHttpPort
property inlocal.settings.json
. Also, in some cases the default port might be blocked with another process (sometimes an olderfunc
itself that didn't terminate).This PR adds support to pick up the next available port (7072, 7073, ...) when none is provided. If a port is defined in either place mentioned above, this would not do anything.
One known issue is that multiple function apps run at the exact same time might end up picking the same port, causing one of them to error out. This is present even today with the static default port, primarily because the validation is done before the host starts up.