hyperlane_plugin_timeout/
fn.rs1use super::*;
2
3pub(crate) fn normalize_prefix(raw: &str) -> String {
13 let trimmed: &str = raw.trim().trim_end_matches('/');
14 if trimmed.is_empty() || trimmed == ROOT_SLASH {
15 return ROOT_SLASH.to_owned();
16 }
17 if trimmed.starts_with('/') {
18 trimmed.to_owned()
19 } else {
20 format!("/{trimmed}")
21 }
22}
23
24#[inline]
35pub(crate) fn path_matches_prefix(path: &str, prefix: &str) -> bool {
36 if prefix == ROOT_SLASH {
37 return true;
38 }
39 if let Some(rest) = path.strip_prefix(prefix) {
40 rest.is_empty() || rest.starts_with('/')
41 } else {
42 false
43 }
44}
45
46pub fn parse_timeout_header_ms(raw: &str) -> Option<u64> {
56 let trimmed: &str = raw.trim();
57 if trimmed.is_empty() {
58 return None;
59 }
60 let ms: u64 = if let Some(s) = trimmed.strip_suffix("ms") {
61 s.trim().parse::<u64>().ok()?
62 } else if let Some(s) = trimmed.strip_suffix('s') {
63 s.trim().parse::<u64>().ok()?.saturating_mul(1000)
64 } else {
65 trimmed.parse::<u64>().ok()?
66 };
67 if ms == 0 { None } else { Some(ms) }
68}
69
70pub async fn timeout_future<F, T>(ctx: &Context, future: F) -> Result<T, TimeoutError>
83where
84 F: Future<Output = T>,
85{
86 let Some(deadline) = RequestDeadline::from_context(ctx) else {
87 return Ok(future.await);
88 };
89 let remaining: Duration = deadline.remaining();
90 if remaining.is_zero() {
91 return Err(TimeoutError::DeadlineExceeded);
92 }
93 timeout(remaining, future)
94 .await
95 .map_err(|_| TimeoutError::DeadlineExceeded)
96}