Skip to content

Commit

Permalink
refactor(multiple): subsitute calls to tracing macros with new intern…
Browse files Browse the repository at this point in the history
…al trace module

Refactor code to utilize tracing macros from the new `trace` module and
fix breaking changes described in commit aba2a35. Part of the effort to
transition `tracing` to an unstable feature hyperium#3326 #hyperium#2874.
  • Loading branch information
RamziA961 committed Sep 28, 2023
1 parent 6bc791e commit d01afc3
Show file tree
Hide file tree
Showing 15 changed files with 13 additions and 200 deletions.
4 changes: 0 additions & 4 deletions src/body/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,9 @@ impl DecodedLength {
/// Checks the `u64` is within the maximum allowed for content-length.
#[cfg(any(feature = "http1", feature = "http2"))]
pub(crate) fn checked_new(len: u64) -> Result<Self, crate::error::Parse> {
#[cfg(feature = "tracing")]
use tracing::warn;

if len <= MAX_LEN {
Ok(DecodedLength(len))
} else {
#[cfg(feature = "tracing")]
warn!("content-length bigger than maximum: {} > {}", len, MAX_LEN);
Err(crate::error::Parse::TooLarge)
}
Expand Down
10 changes: 3 additions & 7 deletions src/client/conn/http1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ where
Err(_canceled) => panic!("dispatch dropped without returning error"),
},
Err(_req) => {
#[cfg(feature = "tracing")]
tracing::debug!("connection was not ready");

debug!("connection was not ready");
Err(crate::Error::new_canceled().with("connection was not ready"))
}
}
Expand All @@ -220,8 +218,7 @@ where
}))
}
Err(req) => {
#[cfg(feature = "tracing")]
tracing::debug!("connection was not ready");
debug!("connection was not ready");
let err = crate::Error::new_canceled().with("connection was not ready");
Either::Right(future::err((err, Some(req))))
}
Expand Down Expand Up @@ -480,8 +477,7 @@ impl Builder {
let opts = self.clone();

async move {
#[cfg(feature = "tracing")]
tracing::trace!("client handshake HTTP/1");
trace!("client handshake HTTP/1");

let (tx, rx) = dispatch::channel();
let mut conn = proto::Conn::new(io);
Expand Down
9 changes: 3 additions & 6 deletions src/client/conn/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ where
Err(_canceled) => panic!("dispatch dropped without returning error"),
},
Err(_req) => {
#[cfg(feature = "tracing")]
tracing::debug!("connection was not ready");
debug!("connection was not ready");

Err(crate::Error::new_canceled().with("connection was not ready"))
}
Expand Down Expand Up @@ -175,8 +174,7 @@ where
}))
}
Err(req) => {
#[cfg(feature = "tracing")]
tracing::debug!("connection was not ready");
debug!("connection was not ready");
let err = crate::Error::new_canceled().with("connection was not ready");
Either::Right(future::err((err, Some(req))))
}
Expand Down Expand Up @@ -409,8 +407,7 @@ where
let opts = self.clone();

async move {
#[cfg(feature = "tracing")]
tracing::trace!("client handshake HTTP/1");
trace!("client handshake HTTP/1");

let (tx, rx) = dispatch::channel();
let h2 = proto::h2::client::handshake(io, rx, &opts.h2_builder, opts.exec, opts.timer)
Expand Down
3 changes: 0 additions & 3 deletions src/client/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use http::{Request, Response};
use http_body::Body;
use pin_project_lite::pin_project;
use tokio::sync::{mpsc, oneshot};
#[cfg(feature = "tracing")]
use tracing::trace;

use crate::{
body::Incoming,
Expand Down Expand Up @@ -317,7 +315,6 @@ where
return std::task::Poll::Pending;
}
};
#[cfg(feature = "tracing")]
trace!("send_when canceled");
Poll::Ready(())
}
Expand Down
32 changes: 0 additions & 32 deletions src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use bytes::{Buf, Bytes};
use http::header::{HeaderValue, CONNECTION};
use http::{HeaderMap, Method, Version};
use httparse::ParserConfig;
#[cfg(feature = "tracing")]
use tracing::{debug, error, trace};

use super::io::Buffered;
use super::{Decoder, Encode, EncodedBuf, Encoder, Http1Transaction, ParseContext, Wants};
Expand Down Expand Up @@ -198,7 +196,6 @@ where
cx: &mut task::Context<'_>,
) -> Poll<Option<crate::Result<(MessageHead<T::Incoming>, DecodedLength, Wants)>>> {
debug_assert!(self.can_read_head());
#[cfg(feature = "tracing")]
trace!("Conn::read_head");

let msg = match ready!(self.io.parse::<T>(
Expand Down Expand Up @@ -230,7 +227,6 @@ where
// Note: don't deconstruct `msg` into local variables, it appears
// the optimizer doesn't remove the extra copies.

#[cfg(feature = "tracing")]
debug!("incoming body is {}", msg.decode);

// Prevent accepting HTTP/0.9 responses after the initial one, if any.
Expand All @@ -253,7 +249,6 @@ where
};

if msg.decode == DecodedLength::ZERO {
#[cfg(feature = "tracing")]
if msg.expect_continue {
debug!("ignoring expect-continue since body is empty");
}
Expand Down Expand Up @@ -281,7 +276,6 @@ where
let was_mid_parse = e.is_parse() || !self.io.read_buf().is_empty();
if was_mid_parse || must_error {
// We check if the buf contains the h2 Preface
#[cfg(feature = "tracing")]
debug!(
"parse error ({}) with {} bytes",
e,
Expand All @@ -292,7 +286,6 @@ where
Err(e) => Poll::Ready(Some(Err(e))),
}
} else {
#[cfg(feature = "tracing")]
debug!("read eof");
self.close_write();
Poll::Ready(None)
Expand All @@ -310,7 +303,6 @@ where
match ready!(decoder.decode(cx, &mut self.io)) {
Ok(slice) => {
let (reading, chunk) = if decoder.is_eof() {
#[cfg(feature = "tracing")]
debug!("incoming body completed");
(
Reading::KeepAlive,
Expand All @@ -321,7 +313,6 @@ where
},
)
} else if slice.is_empty() {
#[cfg(feature = "tracing")]
error!("incoming body unexpectedly ended");
// This should be unreachable, since all 3 decoders
// either set eof=true or return an Err when reading
Expand All @@ -333,7 +324,6 @@ where
(reading, Poll::Ready(chunk))
}
Err(e) => {
#[cfg(feature = "tracing")]
debug!("incoming body decode error: {}", e);
(Reading::Closed, Poll::Ready(Some(Err(e))))
}
Expand All @@ -342,7 +332,6 @@ where
Reading::Continue(ref decoder) => {
// Write the 100 Continue if not already responded...
if let Writing::Init = self.state.writing {
#[cfg(feature = "tracing")]
trace!("automatically sending 100 Continue");
let cont = b"HTTP/1.1 100 Continue\r\n\r\n";
self.io.headers_buf().extend_from_slice(cont);
Expand Down Expand Up @@ -398,7 +387,6 @@ where
debug_assert!(T::is_client());

if !self.io.read_buf().is_empty() {
#[cfg(feature = "tracing")]
debug!("received an unexpected {} bytes", self.io.read_buf().len());
return Poll::Ready(Err(crate::Error::new_unexpected_message()));
}
Expand All @@ -407,11 +395,9 @@ where

if num_read == 0 {
let ret = if self.should_error_on_eof() {
#[cfg(feature = "tracing")]
trace!("found unexpected EOF on busy connection: {:?}", self.state);
Poll::Ready(Err(crate::Error::new_incomplete()))
} else {
#[cfg(feature = "tracing")]
trace!("found EOF on idle connection, closing");
Poll::Ready(Ok(()))
};
Expand All @@ -421,7 +407,6 @@ where
return ret;
}

#[cfg(feature = "tracing")]
debug!(
"received unexpected {} bytes on an idle connection",
num_read
Expand All @@ -440,7 +425,6 @@ where
let num_read = ready!(self.force_io_read(cx)).map_err(crate::Error::new_io)?;

if num_read == 0 {
#[cfg(feature = "tracing")]
trace!("found unexpected EOF on busy connection: {:?}", self.state);
self.state.close_read();
Poll::Ready(Err(crate::Error::new_incomplete()))
Expand All @@ -454,7 +438,6 @@ where

let result = ready!(self.io.poll_read_from_io(cx));
Poll::Ready(result.map_err(|e| {
#[cfg(feature = "tracing")]
trace!("force_io_read; io error = {:?}", e);
self.state.close();
e
Expand Down Expand Up @@ -484,7 +467,6 @@ where
match self.io.poll_read_from_io(cx) {
Poll::Ready(Ok(n)) => {
if n == 0 {
#[cfg(feature = "tracing")]
trace!("maybe_notify; read eof");
if self.state.is_idle() {
self.state.close();
Expand All @@ -495,12 +477,10 @@ where
}
}
Poll::Pending => {
#[cfg(feature = "tracing")]
trace!("maybe_notify; read_from_io blocked");
return;
}
Poll::Ready(Err(e)) => {
#[cfg(feature = "tracing")]
trace!("maybe_notify; read_from_io error: {}", e);
self.state.close();
self.state.error = Some(crate::Error::new_io(e));
Expand Down Expand Up @@ -739,20 +719,17 @@ where
pub(crate) fn poll_flush(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
ready!(Pin::new(&mut self.io).poll_flush(cx))?;
self.try_keep_alive(cx);
#[cfg(feature = "tracing")]
trace!("flushed({}): {:?}", T::LOG, self.state);
Poll::Ready(Ok(()))
}

pub(crate) fn poll_shutdown(&mut self, cx: &mut task::Context<'_>) -> Poll<io::Result<()>> {
match ready!(Pin::new(self.io.io_mut()).poll_shutdown(cx)) {
Ok(()) => {
#[cfg(feature = "tracing")]
trace!("shut down IO complete");
Poll::Ready(Ok(()))
}
Err(e) => {
#[cfg(feature = "tracing")]
debug!("error shutting down IO: {}", e);
Poll::Ready(Err(e))
}
Expand All @@ -772,7 +749,6 @@ where
// If still in Reading::Body, just give up
match self.state.reading {
Reading::Init | Reading::KeepAlive => {
#[cfg(feature = "tracing")]
trace!("body drained")
}
_ => self.close_read(),
Expand All @@ -790,11 +766,9 @@ where
#[cfg(feature = "server")]
pub(crate) fn disable_keep_alive(&mut self) {
if self.state.is_idle() {
#[cfg(feature = "tracing")]
trace!("disable_keep_alive; closing idle connection");
self.state.close();
} else {
#[cfg(feature = "tracing")]
trace!("disable_keep_alive; in-progress connection");
self.state.disable_keep_alive();
}
Expand All @@ -809,7 +783,6 @@ where
}

pub(super) fn on_upgrade(&mut self) -> crate::upgrade::OnUpgrade {
#[cfg(feature = "tracing")]
trace!("{}: prepare possible HTTP upgrade", T::LOG);
self.state.prepare_upgrade()
}
Expand Down Expand Up @@ -926,7 +899,6 @@ impl fmt::Debug for Writing {
impl std::ops::BitAndAssign<bool> for KA {
fn bitand_assign(&mut self, enabled: bool) {
if !enabled {
#[cfg(feature = "tracing")]
trace!("remote disabling keep-alive");
*self = KA::Disabled;
}
Expand Down Expand Up @@ -966,22 +938,19 @@ impl KA {

impl State {
fn close(&mut self) {
#[cfg(feature = "tracing")]
trace!("State::close()");
self.reading = Reading::Closed;
self.writing = Writing::Closed;
self.keep_alive.disable();
}

fn close_read(&mut self) {
#[cfg(feature = "tracing")]
trace!("State::close_read()");
self.reading = Reading::Closed;
self.keep_alive.disable();
}

fn close_write(&mut self) {
#[cfg(feature = "tracing")]
trace!("State::close_write()");
self.writing = Writing::Closed;
self.keep_alive.disable();
Expand All @@ -1001,7 +970,6 @@ impl State {
if let KA::Busy = self.keep_alive.status() {
self.idle::<T>();
} else {
#[cfg(feature = "tracing")]
trace!(
"try_keep_alive({}): could keep-alive, but status = {:?}",
T::LOG,
Expand Down
Loading

0 comments on commit d01afc3

Please sign in to comment.