Skip to content

Commit

Permalink
Change how set_socket_options works to be an extension trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
nfachan committed Oct 21, 2024
1 parent 18c475b commit 090f679
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
22 changes: 14 additions & 8 deletions crates/maelstrom-util/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,18 @@ where
}
}

pub fn set_socket_options(socket: &impl AsRawFd) -> Result<()> {
let fd = Fd::from_raw(socket.as_raw_fd());
linux::setsockopt_tcp_nodelay(&fd, true)?;
linux::setsockopt_so_keepalive(&fd, true)?;
linux::setsockopt_tcp_keepcnt(&fd, 3)?;
linux::setsockopt_tcp_keepidle(&fd, 300)?;
linux::setsockopt_tcp_keepintvl(&fd, 300)?;
Ok(())
pub trait AsRawFdExt: Sized {
fn set_socket_options(self) -> Result<Self>;
}

impl<T: AsRawFd> AsRawFdExt for T {
fn set_socket_options(self) -> Result<Self> {
let fd = Fd::from_raw(self.as_raw_fd());
linux::setsockopt_tcp_nodelay(&fd, true)?;
linux::setsockopt_so_keepalive(&fd, true)?;
linux::setsockopt_tcp_keepcnt(&fd, 3)?;
linux::setsockopt_tcp_keepidle(&fd, 300)?;
linux::setsockopt_tcp_keepintvl(&fd, 300)?;
Ok(self)
}
}
6 changes: 3 additions & 3 deletions crates/maelstrom-worker/src/artifact_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use maelstrom_util::{
cache::{fs::TempFile as _, GotArtifact},
config::common::BrokerAddr,
fs::Fs,
io, net,
io,
net::{self, AsRawFdExt as _},
sync::Pool,
};
use slog::{debug, o, warn, Logger};
Expand Down Expand Up @@ -93,8 +94,7 @@ fn main(
Some(stream) => (stream, true),
None => {
debug!(log, "artifact fetcher connecting to broker");
let mut stream = TcpStream::connect(broker_addr.inner())?;
net::set_socket_options(&stream)?;
let mut stream = TcpStream::connect(broker_addr.inner())?.set_socket_options()?;

net::write_message_to_socket(&mut stream, Hello::ArtifactFetcher, log)?;

Expand Down
11 changes: 6 additions & 5 deletions crates/maelstrom-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use maelstrom_linux::{self as linux};
use maelstrom_util::{
cache::{fs::std::Fs as StdFs, CacheDir},
config::common::Slots,
net, signal,
net::{self, AsRawFdExt as _},
signal,
};
use slog::{debug, error, info, Logger};
use std::{future::Future, process};
Expand All @@ -50,14 +51,14 @@ pub async fn main_inner(config: Config, log: Logger) -> Result<()> {

check_open_file_limit(&log, config.slots, 0)?;

let stream = TcpStream::connect(config.broker.inner())
let (read_stream, mut write_stream) = TcpStream::connect(config.broker.inner())
.await
.map_err(|err| {
error!(log, "error connecting to broker"; "error" => %err);
err
})?;
net::set_socket_options(&stream)?;
let (read_stream, mut write_stream) = stream.into_split();
})?
.set_socket_options()?
.into_split();
let read_stream = BufReader::new(read_stream);

net::write_message_to_async_socket(
Expand Down

0 comments on commit 090f679

Please sign in to comment.