This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto assign placement host port and remove sniffing
- Loading branch information
Showing
5 changed files
with
88 additions
and
67 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
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,26 @@ | ||
using System.Net; | ||
using System.Net.Sockets; | ||
|
||
namespace Microsoft.Tye | ||
{ | ||
public sealed class NextPortFinder | ||
{ | ||
public static NextPortFinder Instance { get; } = new NextPortFinder(); | ||
|
||
private NextPortFinder() | ||
{ | ||
} | ||
|
||
public int GetNextPort() | ||
{ | ||
// Let the OS assign the next available port. Unless we cycle through all ports | ||
// on a test run, the OS will always increment the port number when making these calls. | ||
// This prevents races in parallel test runs where a test is already bound to | ||
// a given port, and a new test is able to bind to the same port due to port | ||
// reuse being enabled by default by the OS. | ||
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0)); | ||
return ((IPEndPoint)socket.LocalEndPoint!).Port; | ||
} | ||
} | ||
} |
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