Skip to content

Commit

Permalink
fix(server): Bind exclusively to the port (#2582)
Browse files Browse the repository at this point in the history
Bind eagerly (and exclusively) to the port for the HTTP server, and fail
to start the Relay otherwise.

The proper error should be also provided, e.g.

```
ERROR relay_server::actors::server: Failed to start the HTTP server: Address already in use (os error 48)
```
  • Loading branch information
olksdr committed Oct 11, 2023
1 parent 260a6f7 commit 4c0acae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Fix reporting of Relay's crashes to Sentry. The `crash-handler` feature did not enable the crash reporter and uploads of crashes were broken. ([#2532](https://github.com/getsentry/relay/pull/2532))
- Use correct field to pick SQL parser for span normalization. ([#2536](https://github.com/getsentry/relay/pull/2536))
- Prevent stack overflow on SQL serialization. ([#2538](https://github.com/getsentry/relay/pull/2538))
- Bind exclusively to the port for the HTTP server. ([#2582](https://github.com/getsentry/relay/pull/2582))

**Internal**:

Expand Down
29 changes: 19 additions & 10 deletions relay-server/src/actors/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::net::SocketAddr;
use std::net::{SocketAddr, TcpListener};
use std::sync::Arc;
use std::time::Duration;

Expand Down Expand Up @@ -111,15 +111,24 @@ impl Service for HttpServer {
.build();

let handle = Handle::new();
let server = axum_server::bind(config.listen_addr())
.http_config(http_config)
.addr_incoming_config(addr_config)
.handle(handle.clone());

relay_log::info!("spawning http server");
relay_log::info!(" listening on http://{}/", config.listen_addr());
relay_statsd::metric!(counter(RelayCounters::ServerStarting) += 1);
tokio::spawn(server.serve(app));
match TcpListener::bind(config.listen_addr()) {
Ok(listener) => {
listener.set_nonblocking(true).ok();
let server = axum_server::from_tcp(listener)
.http_config(http_config)
.addr_incoming_config(addr_config)
.handle(handle.clone());

relay_log::info!("spawning http server");
relay_log::info!(" listening on http://{}/", config.listen_addr());
relay_statsd::metric!(counter(RelayCounters::ServerStarting) += 1);
tokio::spawn(server.serve(app));
}
Err(err) => {
relay_log::error!("Failed to start the HTTP server: {err}");
std::process::exit(1);
}
}

tokio::spawn(async move {
let Shutdown { timeout } = Controller::shutdown_handle().notified().await;
Expand Down

0 comments on commit 4c0acae

Please sign in to comment.