oxidite_middleware/
timeout.rs1use std::time::Duration;
4use std::future::Future;
5use tokio::time::timeout;
6
7pub struct TimeoutMiddleware {
9 duration: Duration,
10}
11
12impl TimeoutMiddleware {
13 pub fn new(duration: Duration) -> Self {
14 Self { duration }
15 }
16
17 pub fn seconds(seconds: u64) -> Self {
18 Self::new(Duration::from_secs(seconds))
19 }
20
21 pub async fn wrap<F, T>(&self, future: F) -> Result<T, TimeoutError>
23 where
24 F: Future<Output = T>,
25 {
26 timeout(self.duration, future)
27 .await
28 .map_err(|_| TimeoutError::Elapsed)
29 }
30}
31
32#[derive(Debug, thiserror::Error)]
33pub enum TimeoutError {
34 #[error("Request timeout elapsed")]
35 Elapsed,
36}
37
38pub struct RequestIdMiddleware {
40 header_name: String,
41}
42
43impl RequestIdMiddleware {
44 pub fn new() -> Self {
45 Self {
46 header_name: "X-Request-ID".to_string(),
47 }
48 }
49
50 pub fn with_header(mut self, header: String) -> Self {
51 self.header_name = header;
52 self
53 }
54
55 pub fn generate_id() -> String {
57 use uuid::Uuid;
58 Uuid::new_v4().to_string()
59 }
60}
61
62impl Default for RequestIdMiddleware {
63 fn default() -> Self {
64 Self::new()
65 }
66}