Skip to content

Commit 88a946a

Browse files
authored
Merge pull request #5982 from Jiloc/chore/add-http-traffic-heartbeat-log
chore: add http traffic heartbeat log
2 parents c98dbcf + c8cb3eb commit 88a946a

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

stackslib/src/net/httpcore.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub const STACKS_REQUEST_ID: &str = "X-Request-Id";
7070
/// from non-Stacks nodes (like Gaia hubs, CDNs, vanilla HTTP servers, and so on).
7171
pub const HTTP_REQUEST_ID_RESERVED: u32 = 0;
7272

73+
/// The interval at which to send heartbeat logs
74+
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(60);
75+
7376
/// All representations of the `tip=` query parameter value
7477
#[derive(Debug, Clone, PartialEq)]
7578
pub enum TipRequest {
@@ -1823,8 +1826,8 @@ pub fn send_http_request(
18231826
stream.set_nodelay(true)?;
18241827

18251828
let start = Instant::now();
1826-
1827-
debug!("send_request: Sending request"; "request" => %request.request_path());
1829+
let request_path = request.request_path();
1830+
debug!("send_request: Sending request"; "request" => request_path);
18281831

18291832
// Some explanation of what's going on here is in order.
18301833
//
@@ -1882,6 +1885,7 @@ pub fn send_http_request(
18821885
.map_err(|e| handle_net_error(e, "Failed to serialize request body"))?;
18831886

18841887
debug!("send_request(sending data)");
1888+
let mut last_heartbeat_time = start; // Initialize heartbeat timer for sending loop
18851889
loop {
18861890
let flushed = request_handle
18871891
.try_flush()
@@ -1900,18 +1904,26 @@ pub fn send_http_request(
19001904
break;
19011905
}
19021906

1903-
if Instant::now().saturating_duration_since(start) > timeout {
1907+
if start.elapsed() >= timeout {
19041908
return Err(io::Error::new(
19051909
io::ErrorKind::WouldBlock,
1906-
"Timed out while receiving request",
1910+
"Timed out while sending request",
19071911
));
19081912
}
1913+
if last_heartbeat_time.elapsed() >= HEARTBEAT_INTERVAL {
1914+
info!(
1915+
"send_request(sending data): heartbeat - still sending request to {} path='{}' (elapsed: {:?})",
1916+
addr, request_path, start.elapsed()
1917+
);
1918+
last_heartbeat_time = Instant::now();
1919+
}
19091920
}
19101921

19111922
// Step 4: pull bytes from the socket back into the handle, and see if the connection decoded
19121923
// and dispatched any new messages to the request handle. If so, then extract the message and
19131924
// check that it's a well-formed HTTP response.
19141925
debug!("send_request(receiving data)");
1926+
last_heartbeat_time = Instant::now();
19151927
let response = loop {
19161928
// get back the reply
19171929
debug!("send_request(receiving data): try to receive data");
@@ -1944,12 +1956,19 @@ pub fn send_http_request(
19441956
};
19451957
request_handle = rh;
19461958

1947-
if Instant::now().saturating_duration_since(start) > timeout {
1959+
if start.elapsed() >= timeout {
19481960
return Err(io::Error::new(
19491961
io::ErrorKind::WouldBlock,
1950-
"Timed out while receiving request",
1962+
"Timed out while receiving response",
19511963
));
19521964
}
1965+
if last_heartbeat_time.elapsed() >= HEARTBEAT_INTERVAL {
1966+
info!(
1967+
"send_request(receiving data): heartbeat - still receiving response from {} path='{}' (elapsed: {:?})",
1968+
addr, request_path, start.elapsed()
1969+
);
1970+
last_heartbeat_time = Instant::now();
1971+
}
19531972
};
19541973

19551974
// Step 5: decode the HTTP message and return it if it's not an error.

0 commit comments

Comments
 (0)