Skip to content

Commit

Permalink
Add connection_verbose setting to log IO events (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar authored Jan 9, 2020
1 parent 20d50da commit 50c33a9
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 54 deletions.
14 changes: 14 additions & 0 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct Config {
#[cfg(feature = "__tls")]
certs_verification: bool,
connect_timeout: Option<Duration>,
connection_verbose: bool,
max_idle_per_host: usize,
#[cfg(feature = "__tls")]
identity: Option<Identity>,
Expand Down Expand Up @@ -111,6 +112,7 @@ impl ClientBuilder {
#[cfg(feature = "__tls")]
certs_verification: true,
connect_timeout: None,
connection_verbose: false,
max_idle_per_host: std::usize::MAX,
proxies: Vec::new(),
auto_sys_proxy: true,
Expand Down Expand Up @@ -234,6 +236,7 @@ impl ClientBuilder {
};

connector.set_timeout(config.connect_timeout);
connector.set_verbose(config.connection_verbose);

let mut builder = hyper::Client::builder();
if config.http2_only {
Expand Down Expand Up @@ -489,6 +492,17 @@ impl ClientBuilder {
self
}

/// Set whether connections should emit verbose logs.
///
/// Enabling this option will emit [log][] messages at the `TRACE` level
/// for read and write operations on connections.
///
/// [log]: https://crates.io/crates/log
pub fn connection_verbose(mut self, verbose: bool) -> ClientBuilder {
self.config.connection_verbose = verbose;
self
}

// HTTP options

/// Sets the maximum idle connection per host allowed in the pool.
Expand Down
38 changes: 2 additions & 36 deletions src/async_impl/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ impl PercentEncoding {
}

fn gen_boundary() -> String {
use crate::util::fast_random as random;

let a = random();
let b = random();
let c = random();
Expand All @@ -495,42 +497,6 @@ fn gen_boundary() -> String {
format!("{:016x}-{:016x}-{:016x}-{:016x}", a, b, c, d)
}

// xor-shift
fn random() -> u64 {
use std::cell::Cell;
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
use std::num::Wrapping;

thread_local! {
static RNG: Cell<Wrapping<u64>> = Cell::new(Wrapping(seed()));
}

fn seed() -> u64 {
let seed = RandomState::new();

let mut out = 0;
let mut cnt = 0;
while out == 0 {
cnt += 1;
let mut hasher = seed.build_hasher();
hasher.write_usize(cnt);
out = hasher.finish();
}
out
}

RNG.with(|rng| {
let mut n = rng.get();
debug_assert_ne!(n.0, 0);
n ^= n >> 12;
n ^= n << 25;
n ^= n >> 27;
rng.set(n);
n.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
10 changes: 10 additions & 0 deletions src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ impl ClientBuilder {
}
}

/// Set whether connections should emit verbose logs.
///
/// Enabling this option will emit [log][] messages at the `TRACE` level
/// for read and write operations on connections.
///
/// [log]: https://crates.io/crates/log
pub fn connection_verbose(self, verbose: bool) -> ClientBuilder {
self.with_inner(move |inner| inner.connection_verbose(verbose))
}

// HTTP options

/// Sets the maximum idle connection per host allowed in the pool.
Expand Down
Loading

0 comments on commit 50c33a9

Please sign in to comment.