Skip to content

Commit 6ef6c41

Browse files
committed
Merge branch 'develop' of https://github.com/stacks-network/stacks-core into feat/signer-state-conflict-resolution-strategies
2 parents 8a7f8fe + 88a946a commit 6ef6c41

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

stacks-common/src/util/macros.rs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,32 @@ macro_rules! iterable_enum {
4040
/// and EnumType.get_name() for free.
4141
#[macro_export]
4242
macro_rules! define_named_enum {
43-
($Name:ident { $($Variant:ident($VarName:literal),)* }) =>
44-
{
43+
(
44+
$(#[$enum_meta:meta])*
45+
$Name:ident {
46+
$(
47+
$(#[$variant_meta:meta])*
48+
$Variant:ident($VarName:literal),
49+
)*
50+
}
51+
) => {
52+
$(#[$enum_meta])*
4553
#[derive(::serde::Serialize, ::serde::Deserialize, Debug, Hash, PartialEq, Eq, Copy, Clone)]
4654
pub enum $Name {
47-
$($Variant),*,
55+
$(
56+
$(#[$variant_meta])*
57+
$Variant,
58+
)*
4859
}
60+
4961
impl $Name {
62+
/// All variants of the enum.
5063
pub const ALL: &[$Name] = &[$($Name::$Variant),*];
64+
65+
/// All names corresponding to the enum variants.
5166
pub const ALL_NAMES: &[&str] = &[$($VarName),*];
5267

68+
/// Looks up a variant by its name string.
5369
pub fn lookup_by_name(name: &str) -> Option<Self> {
5470
match name {
5571
$(
@@ -59,6 +75,7 @@ macro_rules! define_named_enum {
5975
}
6076
}
6177

78+
/// Gets the name of the enum variant as a `String`.
6279
pub fn get_name(&self) -> String {
6380
match self {
6481
$(
@@ -67,6 +84,7 @@ macro_rules! define_named_enum {
6784
}
6885
}
6986

87+
/// Gets the name of the enum variant as a static string slice.
7088
pub fn get_name_str(&self) -> &'static str {
7189
match self {
7290
$(
@@ -75,12 +93,13 @@ macro_rules! define_named_enum {
7593
}
7694
}
7795
}
96+
7897
impl ::std::fmt::Display for $Name {
7998
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
8099
write!(f, "{}", self.get_name_str())
81100
}
82101
}
83-
}
102+
};
84103
}
85104

86105
/// Define a "named" enum, i.e., each variant corresponds
@@ -739,3 +758,46 @@ macro_rules! function_name {
739758
stdext::function_name!()
740759
};
741760
}
761+
762+
#[cfg(test)]
763+
mod tests {
764+
#[test]
765+
fn test_macro_define_named_enum_without_docs() {
766+
define_named_enum!(
767+
MyEnum {
768+
Variant1("variant1"),
769+
Variant2("variant2"),
770+
});
771+
772+
assert_eq!("variant1", MyEnum::Variant1.get_name());
773+
assert_eq!("variant2", MyEnum::Variant2.get_name());
774+
775+
assert_eq!("variant1", MyEnum::Variant1.get_name_str());
776+
assert_eq!("variant2", MyEnum::Variant2.get_name_str());
777+
778+
assert_eq!(Some(MyEnum::Variant1), MyEnum::lookup_by_name("variant1"));
779+
assert_eq!(Some(MyEnum::Variant2), MyEnum::lookup_by_name("variant2"));
780+
assert_eq!(None, MyEnum::lookup_by_name("inexistent"));
781+
}
782+
#[test]
783+
fn test_macro_define_named_enum_with_docs() {
784+
define_named_enum!(
785+
/// MyEnum doc
786+
MyEnum {
787+
/// Variant1 doc
788+
Variant1("variant1"),
789+
/// Variant2 doc
790+
Variant2("variant2"),
791+
});
792+
793+
assert_eq!("variant1", MyEnum::Variant1.get_name());
794+
assert_eq!("variant2", MyEnum::Variant2.get_name());
795+
796+
assert_eq!("variant1", MyEnum::Variant1.get_name_str());
797+
assert_eq!("variant2", MyEnum::Variant2.get_name_str());
798+
799+
assert_eq!(Some(MyEnum::Variant1), MyEnum::lookup_by_name("variant1"));
800+
assert_eq!(Some(MyEnum::Variant2), MyEnum::lookup_by_name("variant2"));
801+
assert_eq!(None, MyEnum::lookup_by_name("inexistent"));
802+
}
803+
}

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)