Skip to content

Commit

Permalink
Add configuration for socket listen queue size (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
is-alnilam authored Aug 31, 2024
1 parent 7ffa516 commit 0d49d10
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Clippy

on: [push]
on: [push, pull_request]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rustfmt.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Rustfmt

on: [push]
on: [push, pull_request]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Tests

on: [push]
on: [push, pull_request]

jobs:
test-latest:
Expand Down
17 changes: 15 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ fn make_app() -> Command {
different help output.",
),
)
.arg(
Arg::new("backlog")
.short('b')
.long("backlog")
.default_value("1")
.value_parser(value_parser!(i32).range(1..))
.value_name("LISTEN-QUEUE")
.help(
"The length of the socket backlog queue in any listen call which \
must be a positive integer greater than or equal to 1. The OS may \
silently cap this value to a lower setting.",
),
)
.arg(
Arg::new("no_pid")
.long("no-pid")
Expand All @@ -57,7 +70,7 @@ fn make_app() -> Command {
"When this is set the LISTEN_PID environment variable is not \
emitted. This is supported by some systems such as the listenfd \
crate to skip the pid check. This is necessary for proxying \
through to other processe like cargo-watch which would break \
through to other processes like cargo-watch which would break \
the pid check. This has no effect on windows.",
),
)
Expand Down Expand Up @@ -120,7 +133,7 @@ pub fn execute() -> Result<(), Error> {
log!("warning: no sockets created");
} else {
for fd in fds {
let raw_fd = fd.create_raw_fd()?;
let raw_fd = fd.create_raw_fd(*matches.get_one("backlog").expect("default value"))?;
raw_fds.push((fd, raw_fd));
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ impl Fd {
}

/// Creates a raw fd from the fd spec.
pub fn create_raw_fd(&self) -> Result<RawFd, anyhow::Error> {
create_raw_fd(self)
pub fn create_raw_fd(&self, listen_backlog: i32) -> Result<RawFd, anyhow::Error> {
create_raw_fd(self, listen_backlog)
}

pub fn describe_raw_fd(&self, raw_fd: RawFd) -> Result<String, anyhow::Error> {
Expand Down Expand Up @@ -148,7 +148,7 @@ mod imp {
use nix::sys::socket::sockopt::ReuseAddr;
use nix::sys::socket::sockopt::ReusePort;

pub fn create_raw_fd(fd: &Fd) -> Result<RawFd, Error> {
pub fn create_raw_fd(fd: &Fd, listen_backlog: i32) -> Result<RawFd, Error> {
let (addr, fam, ty) = sock_info(fd)?;
let sock = socket::socket(fam, ty, socket::SockFlag::empty(), None)?;
setsockopt(sock, ReuseAddr, &true)?;
Expand All @@ -163,7 +163,7 @@ mod imp {
.map_err(From::from)
.and_then(|_| {
if fd.should_listen() {
socket::listen(sock, 1)?;
socket::listen(sock, listen_backlog as usize)?;
}
Ok(())
});
Expand Down Expand Up @@ -234,13 +234,13 @@ mod imp {

use anyhow::{bail, Error};

pub fn create_raw_fd(fd: &Fd) -> Result<RawFd, Error> {
pub fn create_raw_fd(fd: &Fd, listen_backlog: i32) -> Result<RawFd, Error> {
let (addr, dom, ty) = sock_info(fd)?;
let sock = socket2::Socket::new(dom, ty, None)?;

sock.bind(&addr)?;
if fd.should_listen() {
sock.listen(1)?;
sock.listen(listen_backlog as _)?;
}

Ok(sock.into_raw_socket())
Expand Down

0 comments on commit 0d49d10

Please sign in to comment.