Skip to main content

tower_bulkhead/
events.rs

1//! Event types for bulkhead pattern.
2
3use std::time::{Duration, Instant};
4use tower_resilience_core::events::ResilienceEvent;
5
6/// Events emitted by the bulkhead pattern.
7#[derive(Debug, Clone)]
8pub enum BulkheadEvent {
9    /// A call was permitted through the bulkhead.
10    CallPermitted {
11        /// Name of the bulkhead instance.
12        pattern_name: String,
13        /// When the event occurred.
14        timestamp: Instant,
15        /// Current number of concurrent calls.
16        concurrent_calls: usize,
17    },
18    /// A call was rejected because the bulkhead is full.
19    CallRejected {
20        /// Name of the bulkhead instance.
21        pattern_name: String,
22        /// When the event occurred.
23        timestamp: Instant,
24        /// Maximum concurrent calls allowed.
25        max_concurrent_calls: usize,
26    },
27    /// A call finished successfully.
28    CallFinished {
29        /// Name of the bulkhead instance.
30        pattern_name: String,
31        /// When the event occurred.
32        timestamp: Instant,
33        /// Duration of the call.
34        duration: Duration,
35    },
36    /// A call finished with an error.
37    CallFailed {
38        /// Name of the bulkhead instance.
39        pattern_name: String,
40        /// When the event occurred.
41        timestamp: Instant,
42        /// Duration of the call.
43        duration: Duration,
44    },
45}
46
47impl ResilienceEvent for BulkheadEvent {
48    fn event_type(&self) -> &'static str {
49        match self {
50            BulkheadEvent::CallPermitted { .. } => "call_permitted",
51            BulkheadEvent::CallRejected { .. } => "call_rejected",
52            BulkheadEvent::CallFinished { .. } => "call_finished",
53            BulkheadEvent::CallFailed { .. } => "call_failed",
54        }
55    }
56
57    fn timestamp(&self) -> Instant {
58        match self {
59            BulkheadEvent::CallPermitted { timestamp, .. }
60            | BulkheadEvent::CallRejected { timestamp, .. }
61            | BulkheadEvent::CallFinished { timestamp, .. }
62            | BulkheadEvent::CallFailed { timestamp, .. } => *timestamp,
63        }
64    }
65
66    fn pattern_name(&self) -> &str {
67        match self {
68            BulkheadEvent::CallPermitted { pattern_name, .. }
69            | BulkheadEvent::CallRejected { pattern_name, .. }
70            | BulkheadEvent::CallFinished { pattern_name, .. }
71            | BulkheadEvent::CallFailed { pattern_name, .. } => pattern_name,
72        }
73    }
74}