r2d2/
event.rs

1//! Event subscriptions.
2
3use std::fmt;
4use std::time::Duration;
5
6/// A trait which is provided with information about events in a connection pool.
7pub trait HandleEvent: fmt::Debug + Sync + Send {
8    /// Called when a new connection is acquired.
9    ///
10    /// The default implementation does nothing.
11    #[allow(unused_variables)]
12    fn handle_acquire(&self, event: AcquireEvent) {}
13
14    /// Called when a connection is released.
15    ///
16    /// The default implementation does nothing.
17    #[allow(unused_variables)]
18    fn handle_release(&self, event: ReleaseEvent) {}
19
20    /// Called when a connection is checked out from the pool.
21    ///
22    /// The default implementation does nothing.
23    #[allow(unused_variables)]
24    fn handle_checkout(&self, event: CheckoutEvent) {}
25
26    /// Called when a checkout attempt times out.
27    ///
28    /// The default implementation does nothing.
29    #[allow(unused_variables)]
30    fn handle_timeout(&self, event: TimeoutEvent) {}
31
32    /// Called when a connection is checked back into the pool.
33    #[allow(unused_variables)]
34    fn handle_checkin(&self, event: CheckinEvent) {}
35}
36
37/// A `HandleEvent` implementation which does nothing.
38#[derive(Copy, Clone, Debug)]
39pub struct NopEventHandler;
40
41impl HandleEvent for NopEventHandler {}
42
43/// Information about an acquire event.
44#[derive(Debug)]
45pub struct AcquireEvent {
46    pub(crate) id: u64,
47}
48
49impl AcquireEvent {
50    /// Returns the ID of the connection.
51    #[inline]
52    pub fn connection_id(&self) -> u64 {
53        self.id
54    }
55}
56
57/// Information about a release event.
58#[derive(Debug)]
59pub struct ReleaseEvent {
60    pub(crate) id: u64,
61    pub(crate) age: Duration,
62}
63
64impl ReleaseEvent {
65    /// Returns the ID of the connection.
66    #[inline]
67    pub fn connection_id(&self) -> u64 {
68        self.id
69    }
70
71    /// Returns the age of the connection.
72    #[inline]
73    pub fn age(&self) -> Duration {
74        self.age
75    }
76}
77
78/// Information about a checkout event.
79#[derive(Debug)]
80pub struct CheckoutEvent {
81    pub(crate) id: u64,
82    pub(crate) duration: Duration,
83}
84
85impl CheckoutEvent {
86    /// Returns the ID of the connection.
87    #[inline]
88    pub fn connection_id(&self) -> u64 {
89        self.id
90    }
91
92    /// Returns the time taken to check out the connection.
93    #[inline]
94    pub fn duration(&self) -> Duration {
95        self.duration
96    }
97}
98
99/// Information about a timeout event.
100#[derive(Debug)]
101pub struct TimeoutEvent {
102    pub(crate) timeout: Duration,
103}
104
105impl TimeoutEvent {
106    /// Returns the timeout of the failed checkout attempt.
107    #[inline]
108    pub fn timeout(&self) -> Duration {
109        self.timeout
110    }
111}
112
113/// Information about a checkin event.
114#[derive(Debug)]
115pub struct CheckinEvent {
116    pub(crate) id: u64,
117    pub(crate) duration: Duration,
118}
119
120impl CheckinEvent {
121    /// Returns the ID of the connection.
122    #[inline]
123    pub fn connection_id(&self) -> u64 {
124        self.id
125    }
126
127    /// Returns the amount of time the connection was checked out.
128    #[inline]
129    pub fn duration(&self) -> Duration {
130        self.duration
131    }
132}