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

Use next available port when the default port is already used #2682

Open
wants to merge 3 commits into
base: v3.x
Choose a base branch
from

Conversation

PramodValavala-MSFT
Copy link
Contributor

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 the Host.LocalHttpPort property in local.settings.json. Also, in some cases the default port might be blocked with another process (sometimes an older func 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.

@kshyju kshyju self-assigned this Jul 10, 2024
@liliankasem
Copy link
Member

@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();

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,

  1. Used HashSet and iterate over all the values and lookup will be O(N), then you can do startPort++ and keep looking.
  2. Used TreeSet or SortedSet to store and look for value less than startPort.

}
startPort++;
}

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)
{

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

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

Successfully merging this pull request may close these issues.

4 participants