This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding better port detection (#260)
- Loading branch information
Showing
8 changed files
with
52 additions
and
72 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
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,22 @@ | ||
use std::net::SocketAddr; | ||
use std::net::{TcpListener, TcpStream}; | ||
|
||
pub fn get_available_port() -> u16 { | ||
get_available_addr().port() | ||
} | ||
|
||
pub fn get_available_addr() -> SocketAddr { | ||
let host = "127.0.0.1"; | ||
|
||
let listener = TcpListener::bind((host, 0)).expect("Can't bind to an available port"); | ||
let addr = listener | ||
.local_addr() | ||
.expect("Can't extract local addr from listener"); | ||
|
||
// Forcing the port into the TIME_WAIT state is necessary to ensure that the port will be | ||
// reserved from some limited amount of time (roughly 60s on some Linux systems) | ||
let _sender = TcpStream::connect(addr).expect("Can't connect to an available port"); | ||
let _incoming = listener.accept().expect("Can't accept connection"); | ||
|
||
addr | ||
} |
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