Repository navigation
Gateway - Add port-hint cli arg to enable restarting on the same port #168
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
|
seanpar203 marked this conversation as resolved.
|
This file contains hidden or 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,124 @@ | ||
| use std::net::SocketAddr; | ||
|
|
||
| use super::*; | ||
|
|
||
| /// A get a port that was is not in use. | ||
| fn free_port() -> u16 { | ||
| std::net::TcpListener::bind("127.0.0.1:0") | ||
| .expect("should bind a probe") | ||
| .local_addr() | ||
| .expect("address should exist") | ||
| .port() | ||
| } | ||
|
|
||
| fn loopback(port: u16) -> SocketAddr { | ||
| SocketAddr::from(([127, 0, 0, 1], port)) | ||
| } | ||
|
|
||
| /// create a tokio Runtime for a test. | ||
| fn runtime() -> tokio::runtime::Runtime { | ||
| tokio::runtime::Builder::new_current_thread() | ||
| .enable_io() | ||
| .build() | ||
| .expect("a runtime builds") | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_port_zero_config_binds_the_preferred_port() { | ||
| let runtime = runtime(); | ||
|
|
||
| let preferred_port = free_port(); | ||
|
|
||
| let listener = | ||
| bind_listener(loopback(0), Some(preferred_port), runtime.handle()).expect("should bind"); | ||
|
|
||
| let listener_port = listener | ||
| .local_addr() | ||
| .expect("should have an address") | ||
| .port(); | ||
|
|
||
| assert_eq!(listener_port, preferred_port); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_configured_port_wins_over_the_preferred_port() { | ||
| let runtime = runtime(); | ||
|
|
||
| let configured_port = free_port(); | ||
| let preferred_port = free_port(); | ||
|
|
||
| let listener = bind_listener( | ||
| loopback(configured_port), | ||
| Some(preferred_port), | ||
| runtime.handle(), | ||
| ) | ||
| .expect("should bind"); | ||
|
|
||
| let listener_port = listener | ||
| .local_addr() | ||
| .expect("should have an address") | ||
| .port(); | ||
|
|
||
| assert_eq!(listener_port, configured_port); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_taken_preferred_port_falls_back_to_a_free_one() { | ||
| let runtime = runtime(); | ||
|
|
||
| let holder = std::net::TcpListener::bind("127.0.0.1:0").expect("should bind"); | ||
| let used_port = holder.local_addr().expect("should have an address").port(); | ||
|
|
||
| let listener = | ||
| bind_listener(loopback(0), Some(used_port), runtime.handle()).expect("should bind"); | ||
|
|
||
| let listener_port = listener | ||
| .local_addr() | ||
| .expect("should have an address") | ||
| .port(); | ||
|
|
||
| assert_ne!(listener_port, used_port); | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_preferred_port_binds_a_free_port() { | ||
| let runtime = runtime(); | ||
|
|
||
| let listener = bind_listener(loopback(0), None, runtime.handle()).expect("binds"); | ||
|
|
||
| let listener_port = listener | ||
| .local_addr() | ||
| .expect("should have an address") | ||
| .port(); | ||
|
|
||
| assert_ne!(listener_port, 0); | ||
| } | ||
|
|
||
| /// The server side closing a connection first leaves it in `TIME_WAIT`, | ||
| /// which is what a gateway shutting down with clients attached does. | ||
| #[tokio::test] | ||
| async fn a_released_port_rebinds_while_its_connection_is_in_time_wait() { | ||
| let runtime = tokio::runtime::Handle::current(); | ||
|
|
||
| let listener = bind_listener(loopback(0), None, &runtime).expect("should bind"); | ||
|
|
||
| let port = listener | ||
| .local_addr() | ||
| .expect("should have an address") | ||
| .port(); | ||
|
|
||
| let client = tokio::net::TcpStream::connect(loopback(port)) | ||
| .await | ||
| .expect("should connect"); | ||
| let (served, _) = listener.accept().await.expect("should accept"); | ||
| drop(served); | ||
| drop(listener); | ||
| drop(client); | ||
|
|
||
| let rebound = bind_listener(loopback(0), Some(port), &runtime).expect("should rebind"); | ||
|
|
||
| assert_eq!( | ||
| rebound.local_addr().expect("should have an address").port(), | ||
| port | ||
| ); | ||
| } |
This file contains hidden or 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,72 @@ | ||
| //! Binding the gateway's listener to: | ||
| //! - The configured address, if it is set | ||
| //! - The preferred port if it is set and available | ||
| //! - Any available port | ||
|
|
||
| use std::net::SocketAddr; | ||
|
|
||
| use tokio::{ | ||
| net::{TcpListener, TcpSocket}, | ||
| runtime::EnterGuard, | ||
| }; | ||
|
|
||
| const MAX_BACKLOG_CONNECTIONS: u32 = 1024; | ||
|
|
||
| /// Binds the gateway's listener to `addr`. | ||
| /// | ||
| /// When `addr` asks for any free port (port 0) and `preferred_port` names one, | ||
| /// that port is tried first, enabling a gateway relaunched by its supervisor to | ||
| /// come back on the address its clients already hold. If the preferred port is | ||
| /// taken, a free port is bound instead and a warning is logged. `preferred_port` is | ||
| /// ignored if `addr` asks for a port other than 0. | ||
| pub(super) fn bind_listener( | ||
| addr: SocketAddr, | ||
| preferred_port: Option<u16>, | ||
| runtime: &tokio::runtime::Handle, | ||
| ) -> std::io::Result<TcpListener> { | ||
| let context = runtime.enter(); | ||
|
|
||
| if addr.port() == 0 | ||
| && let Some(preferred_port) = preferred_port | ||
| { | ||
| let preferred_addr = SocketAddr::new(addr.ip(), preferred_port); | ||
|
|
||
| match bind_addr(preferred_addr, &context) { | ||
| Ok(listener) => return Ok(listener), | ||
| Err(error) => tracing::warn!( | ||
| "could not bind the preferred port {preferred_port}: {error}; binding a free port instead" | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| bind_addr(addr, &context) | ||
| } | ||
|
|
||
| /// Binds `address`, reusing a port even if its previous connections are still in | ||
| /// `TIME_WAIT`, so a restarted gateway can take back the port it just released. | ||
| /// | ||
| /// Must run inside a tokio runtime context, which the caller supplies via the | ||
| /// `context` param | ||
| fn bind_addr(address: SocketAddr, _context: &EnterGuard<'_>) -> std::io::Result<TcpListener> { | ||
| let socket = if address.is_ipv4() { | ||
| TcpSocket::new_v4()? | ||
| } else { | ||
| TcpSocket::new_v6()? | ||
| }; | ||
|
|
||
| // Unix only: on Windows `SO_REUSEADDR` lets a second socket take a port that is in use | ||
| #[cfg(unix)] | ||
| socket.set_reuseaddr(true)?; | ||
|
|
||
| socket.bind(address)?; | ||
|
|
||
| // backlog connections defines the maximum number of pending connections | ||
| // queued by the operating system at any given time. Connection are removed | ||
| // from the queue with `TcpListener::accept`. When the queue is full, the | ||
| // operating-system will start rejecting connections. | ||
| socket.listen(MAX_BACKLOG_CONNECTIONS) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "bind-tests.rs"] | ||
| mod tests; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.