Skip to content

Commit a361d9f

Browse files
committed
chore: Cache reponse_handler_index after looking it up for metrics identifier
1 parent 9c4a445 commit a361d9f

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

stackslib/src/monitoring/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use stacks_common::util::uint::{Uint256, Uint512};
2828

2929
use crate::burnchains::{BurnchainSigner, Txid};
3030
use crate::core::MemPoolDB;
31-
use crate::net::httpcore::{StacksHttp, StacksHttpRequest, StacksHttpResponse};
31+
use crate::net::httpcore::{StacksHttpRequest, StacksHttpResponse};
3232
use crate::net::rpc::ConversationHttp;
3333
use crate::net::Error as net_error;
3434
use crate::util_lib::db::{sqlite_open, tx_busy_handler, DBConn, Error as DatabaseError};
@@ -48,7 +48,7 @@ pub fn increment_rpc_calls_counter() {
4848

4949
pub fn instrument_http_request_handler<F, R>(
5050
conv_http: &mut ConversationHttp,
51-
req: StacksHttpRequest,
51+
mut req: StacksHttpRequest,
5252
handler: F,
5353
) -> Result<R, net_error>
5454
where
@@ -58,7 +58,7 @@ where
5858
increment_rpc_calls_counter();
5959

6060
#[cfg(feature = "monitoring_prom")]
61-
let timer = prometheus::new_rpc_call_timer(conv_http.metrics_identifier(&req));
61+
let timer = prometheus::new_rpc_call_timer(conv_http.metrics_identifier(&mut req));
6262

6363
let res = handler(conv_http, req);
6464

stackslib/src/net/httpcore.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ pub struct StacksHttpRequest {
438438
preamble: HttpRequestPreamble,
439439
contents: HttpRequestContents,
440440
start_time: u128,
441+
/// Cache result of `StacksHttp::find_response_handler` so we don't have to do the regex matching twice
442+
response_handler_index: Option<usize>,
441443
}
442444

443445
impl StacksHttpRequest {
@@ -446,6 +448,7 @@ impl StacksHttpRequest {
446448
preamble,
447449
contents,
448450
start_time: get_epoch_time_ms(),
451+
response_handler_index: None,
449452
}
450453
}
451454

@@ -481,6 +484,7 @@ impl StacksHttpRequest {
481484
preamble,
482485
contents,
483486
start_time: get_epoch_time_ms(),
487+
response_handler_index: None,
484488
})
485489
}
486490

@@ -912,9 +916,7 @@ impl StacksHttp {
912916
if request_verb != verb {
913917
continue;
914918
}
915-
let _captures = if let Some(caps) = regex.captures(request_path) {
916-
caps
917-
} else {
919+
let Some(_captures) = regex.captures(request_path) else {
918920
continue;
919921
};
920922

@@ -985,9 +987,7 @@ impl StacksHttp {
985987
if &preamble.verb != verb {
986988
continue;
987989
}
988-
let captures = if let Some(caps) = regex.captures(&decoded_path) {
989-
caps
990-
} else {
990+
let Some(captures) = regex.captures(&decoded_path) else {
991991
continue;
992992
};
993993

@@ -1085,21 +1085,18 @@ impl StacksHttp {
10851085
node: &mut StacksNodeState,
10861086
) -> Result<(HttpResponsePreamble, HttpResponseContents), NetError> {
10871087
let (decoded_path, _) = decode_request_path(&request.preamble().path_and_query_str)?;
1088-
let response_handler_index =
1089-
if let Some(i) = self.find_response_handler(&request.preamble().verb, &decoded_path) {
1090-
i
1091-
} else {
1092-
// method not found
1093-
return StacksHttpResponse::new_error(
1094-
&request.preamble,
1095-
&HttpNotFound::new(format!(
1096-
"No such API endpoint '{} {}'",
1097-
&request.preamble().verb,
1098-
&decoded_path
1099-
)),
1100-
)
1101-
.try_into_contents();
1102-
};
1088+
let Some(response_handler_index) = request.response_handler_index.or_else(|| self.find_response_handler(&request.preamble().verb, &decoded_path)) else {
1089+
// method not found
1090+
return StacksHttpResponse::new_error(
1091+
&request.preamble,
1092+
&HttpNotFound::new(format!(
1093+
"No such API endpoint '{} {}'",
1094+
&request.preamble().verb,
1095+
&decoded_path
1096+
)),
1097+
)
1098+
.try_into_contents();
1099+
};
11031100

11041101
let (_, _, request_handler) = self
11051102
.request_handlers
@@ -1246,15 +1243,17 @@ impl StacksHttp {
12461243
/// Get a unique `&str` identifier for each request type
12471244
/// This can only return a finite set of identifiers, which makes it safer to use for Prometheus metrics
12481245
/// For details see https://github.com/stacks-network/stacks-core/issues/4574
1249-
pub fn metrics_identifier(&self, req: &StacksHttpRequest) -> &str {
1246+
pub fn metrics_identifier(&self, req: &mut StacksHttpRequest) -> &str {
12501247
let Ok((decoded_path, _)) = decode_request_path(&req.request_path()) else {
12511248
return "<err-url-decode>";
12521249
};
1253-
let Some(response_handler_index) =
1254-
self.find_response_handler(&req.preamble().verb, &decoded_path)
1250+
1251+
let Some(response_handler_index) = req.response_handler_index
1252+
.or_else(|| self.find_response_handler(&req.preamble().verb, &decoded_path))
12551253
else {
12561254
return "<err-handler-not-found>";
12571255
};
1256+
req.response_handler_index = Some(response_handler_index);
12581257

12591258
let (_, _, request_handler) = self
12601259
.request_handlers

stackslib/src/net/rpc.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,8 @@ impl ConversationHttp {
534534
test_debug!("{:?}: {} HTTP requests pending", &self, num_inbound);
535535

536536
for _i in 0..num_inbound {
537-
let msg = match self.connection.next_inbox_message() {
538-
None => {
539-
continue;
540-
}
541-
Some(m) => m,
537+
let Some(msg) = self.connection.next_inbox_message() else {
538+
continue;
542539
};
543540

544541
match msg {
@@ -661,7 +658,7 @@ impl ConversationHttp {
661658
self.peer_host.clone()
662659
}
663660

664-
pub fn metrics_identifier(&self, req: &StacksHttpRequest) -> &str {
661+
pub fn metrics_identifier(&self, req: &mut StacksHttpRequest) -> &str {
665662
self.connection.protocol.metrics_identifier(req)
666663
}
667664
}

0 commit comments

Comments
 (0)