Skip to main content

TcpOpts

Struct TcpOpts 

Source
pub struct TcpOpts {
    pub nodelay: bool,
    pub keepalive: Option<Duration>,
    pub keepalive_interval: Option<Duration>,
    pub keepalive_retries: Option<u32>,
    pub bind_device: Option<String>,
    pub user_timeout: Option<Duration>,
    pub local_address: Option<IpAddr>,
    pub send_buffer_size: Option<usize>,
    pub recv_buffer_size: Option<usize>,
    pub reuse_address: bool,
}
Expand description

Socket options are applied in hclient once, on the socket2::Socket, and the runtime only adopts the descriptor (TcpAdoptStd). Otherwise every runtime crate would rewrite this whole rigmarole again.

§default() is all-off, and that was re-decided rather than inherited

Nagle’s algorithm costs the head of a Native TLS exchange 41 ms on loopback — measured from the server’s side of the wire in hclient-native’s tests/nagle_cost.rs, and 0.9 ms with nodelay set. Every field here stays false/None anyway, for two reasons that are not caution:

  • This is a socket seam, and it does not know who is writing. The 41 ms is the write-write-read pattern of a request over TLS meeting a peer’s delayed ACK. A protocol that streams one way is exactly the one Nagle helps, and a default here would impose one caller’s protocol on every other caller of the trait.
  • A set option is a refusal, not a preference. TcpOpts::reject_unsupported fails the connect on a runtime whose TcpConnect::APPLIES does not cover it, and that default is NONE. Turning a field on here would turn every connect on a backend that forgot to declare APPLIES into an Unsupported error for an option its caller never mentioned — a performance fix aimed straight at the implementors the NONE default was written to protect.

So the opinion lives where the protocol is: hclient_native::Native::new asks for nodelay, and asks only where the runtime declares it applies it.

Fields§

§nodelay: bool

TCP_NODELAY — Nagle’s algorithm off. See the type’s own doc for why default() leaves it false and who turns it on.

§keepalive: Option<Duration>

TCP_KEEPIDLE — how long a connection may be idle before the first probe.

One setting in three parts, with keepalive_interval and keepalive_retries. Setting any of the three turns SO_KEEPALIVE on; each part left None keeps the operating system’s value for it. That is socket2::TcpKeepalive’s own shape and it is stated here because the field names do not say it: a caller who sets only the interval has switched keepalive on, with the OS’s idle time.

§keepalive_interval: Option<Duration>

TCP_KEEPINTVL — the gap between probes once they have started.

Worth setting with keepalive rather than instead of it: the idle time decides when a dead peer starts being noticed and this decides how fast the noticing then goes, and Linux’s defaults are 7200 s and 75 s, so an untouched idle time makes the interval nearly irrelevant.

§keepalive_retries: Option<u32>

TCP_KEEPCNT — how many unanswered probes end the connection.

§bind_device: Option<String>

SO_BINDTODEVICE — the interface this socket must use, by name.

Not local_address under another name: an address binds the source address, and the kernel still routes by its table, so a request can leave through a different interface that happens to hold the same address. This binds the interface, which is what a caller on a multi-homed host or inside a VRF actually means. Linux, Android and Fuchsia only — see TcpOptsSupport, which is where a runtime says so per target.

A String rather than a &'static str because an interface name is configuration a caller reads at run time, and rather than bytes because every interface name on every platform that has this option is ASCII.

§user_timeout: Option<Duration>

TCP_USER_TIMEOUT — how long transmitted data may stay unacknowledged before the connection is dropped.

The one option here that catches a peer which vanished mid-transfer, where keepalive only catches an idle one: probes are sent when nothing is in flight, so a connection with unsent acknowledgements sits in retransmission for minutes with keepalive never firing. Linux, Android, Fuchsia and Cygwin only.

It overlaps Timeouts::between_bytes and does not replace it: this is the kernel’s, applies to a socket rather than to an exchange, and is the only one of the two that a build with no Client above it can reach.

§local_address: Option<IpAddr>§send_buffer_size: Option<usize>§recv_buffer_size: Option<usize>§reuse_address: bool

Implementations§

Source§

impl TcpOpts

Source

pub fn reject_unsupported(&self, can: TcpOptsSupport) -> Result<()>

Fail when the caller set an option can says this runtime does not apply — the one sanctioned answer to an option a runtime cannot honour, since silently ignoring it is not one.

Only fields that are actually set can offend: TcpOpts::default is all-off, so even a runtime with TcpOptsSupport::NONE still serves every caller that never asked for anything.

A runtime whose TcpConnect::APPLIES is TcpOptsSupport::ALL need not call this at all — the call is a no-op by construction, which reject_unsupported_is_a_no_op_against_all pins.

Trait Implementations§

Source§

impl Clone for TcpOpts

Source§

fn clone(&self) -> TcpOpts

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TcpOpts

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TcpOpts

Source§

fn default() -> TcpOpts

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.