Skip to content
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

More TCP config using net2 #794

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ jobs:
feature:
- ""
- charset
- native-tls
- cookies
- socks-proxy
- gzip
- brotli
- json
- net2
env:
RUST_BACKTRACE: "1"
RUSTFLAGS: "-D dead_code -D unused-variables -D unused"
Expand Down
30 changes: 24 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ rust-version = "1.67"
features = ["rustls", "native-tls", "socks-proxy", "cookies", "gzip", "brotli", "charset", "json", "_test"]

[features]
default = ["rustls", "native-tls", "socks-proxy", "cookies", "gzip", "brotli", "charset", "json"]
default = ["rustls", "gzip", "json"]
full = ["rustls", "native-tls", "socks-proxy", "cookies", "gzip", "brotli", "charset", "json", "net2"]
rustls = ["dep:rustls", "_tls", "dep:rustls-platform-verifier", "dep:webpki-roots"]
native-tls = ["dep:native-tls", "dep:der", "_tls", "dep:webpki-root-certs"]
socks-proxy = ["dep:socks"]
Expand All @@ -28,6 +29,7 @@ gzip = ["dep:flate2"]
brotli = ["dep:brotli-decompressor"]
charset = ["dep:encoding_rs"]
json = ["dep:serde", "dep:serde_json"]
net2 = ["dep:net2"]

# Underscore prefixed features are internal
_url = ["dep:url"]
Expand Down Expand Up @@ -71,6 +73,8 @@ encoding_rs = { version = "0.8.34", optional = true }
serde = { version = "1.0.204", optional = true, default-features = false, features = ["std"] }
serde_json = { version = "1.0.120", optional = true, default-features = false, features = ["std"] }

net2 = { version = "0.2.39", optional = true, default-features = false, features = ["duration"] }

[build-dependencies]
cc = "1.0.106"

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ The default enabled features are: **rustls**, **gzip** and **json**.
library defaults to Rust's built in `utf-8`.
* **json** enables JSON sending and receiving via serde_json.

* **full** enables all the optional features.

## JSON

By enabling the **json** feature, the library supports serde json.
Expand Down
34 changes: 34 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ pub struct AgentConfig {
/// Defaults to `true`.
pub no_delay: bool,

/// Set the TCP keep alive duration.
///
/// On Unix, this option will set the `SO_KEEPALIVE` as well as the `TCP_KEEPALIVE` or
/// `TCP_KEEPIDLE` option (depending on your platform). On Windows, this will set the
/// `SIO_KEEPALIVE_VALS` option.
///
/// This does nothing for SOCKS connections.
///
/// Defaults to `None`.
#[cfg(feature = "net2")]
pub tcp_keepalive: Option<Duration>,

/// Set IP_TTL configuration on the socket.
///
/// This does nothing for SOCKS connections.
///
/// Defaults to `None`.
#[cfg(feature = "net2")]
pub ttl: Option<u32>,

/// Set the SO_LINGER configuration on the socket.
///
/// This does nothing for SOCKS connections.
///
/// Defaults to `None`.
#[cfg(feature = "net2")]
pub linger: Option<Duration>,

/// The max number of redirects to follow before giving up
///
/// Defaults to 10
Expand Down Expand Up @@ -241,6 +269,12 @@ impl Default for AgentConfig {
tls_config: TlsConfig::default(),
proxy: Proxy::try_from_env(),
no_delay: true,
#[cfg(feature = "net2")]
tcp_keepalive: None,
#[cfg(feature = "net2")]
ttl: None,
#[cfg(feature = "net2")]
linger: None,
max_redirects: 10,
redirect_auth_headers: RedirectAuthHeaders::Never,
user_agent: "ureq".to_string(), // TODO(martin): add version
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@
//! (e.g. `Content-Type: text/plain; charset=iso-8859-1`). Without this, the
//! library defaults to Rust's built in `utf-8`.
//! * **json** enables JSON sending and receiving via serde_json.
//! * **net2** allows for more TCP configuration such as `TCP_KEEPALIVE`, `IP_TTL` and `SO_LINGER`.
//!
//! * **full** enables all the optional features.
//!
//! # JSON
//!
Expand Down
15 changes: 15 additions & 0 deletions src/transport/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ fn try_connect_single(
stream.set_nodelay(true)?;
}

#[cfg(feature = "net2")]
{
if let Some(keepalive) = config.tcp_keepalive {
net2::TcpStreamExt::set_keepalive(&stream, Some(keepalive))?;
}

if let Some(ttl) = config.ttl {
net2::TcpStreamExt::set_ttl(&stream, ttl)?;
}

if let Some(linger) = config.linger {
net2::TcpStreamExt::set_linger(&stream, Some(linger))?;
}
}

debug!("Connected TcpStream to {}", addr);

Ok(stream)
Expand Down
Loading