1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! Pluggable instrumentation
use std::fmt;
use std::sync::Arc;
use std::time::Duration;

#[cfg(feature = "metrix")]
pub use self::metrix::{MetrixConfig, MetrixInstrumentation};
pub use state_counters::*;

#[cfg(feature = "metrix")]
mod metrix;
mod state_counters;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PoolId(usize);

impl PoolId {
    pub fn new(id: usize) -> Self {
        PoolId(id)
    }

    pub fn into_inner(self) -> usize {
        self.0
    }

    pub fn inc(&mut self) {
        self.0 += 1;
    }
}

impl From<PoolId> for usize {
    fn from(pid: PoolId) -> Self {
        pid.0
    }
}

impl fmt::Display for PoolId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "P{:04}", self.0)
    }
}

/// A trait with methods that get called by the pool on certain events.
///
pub trait Instrumentation {
    fn pool_added(&self, pool: PoolId);

    fn pool_removed(&self, pool: PoolId);

    /// A connection was checked out
    fn checked_out_connection(
        &self,
        idle_for: Duration,
        time_since_checkout_request: Duration,
        pool: PoolId,
    );

    /// A connection that was previously checked out was checked in again
    fn checked_in_returned_connection(&self, flight_time: Duration, pool: PoolId);

    /// A newly created connection was checked in
    fn checked_in_new_connection(&self, pool: PoolId);

    /// A connection was dropped because it was marked as defect
    fn connection_dropped(&self, flight_time: Option<Duration>, lifetime: Duration, pool: PoolId);

    /// A new connection was created
    fn connection_created(&self, connected_after: Duration, total_time: Duration, pool: PoolId);

    /// The number of idle connections increased by 1
    fn idle_inc(&self, pool: PoolId);

    /// The number of idle connections decreased by 1
    fn idle_dec(&self, pool: PoolId);

    /// The number of in flight connections increased by 1
    fn in_flight_inc(&self, pool: PoolId);

    /// The number of in flight connections decreased by 1
    fn in_flight_dec(&self, pool: PoolId);

    /// A reservation has been enqueued
    fn reservation_added(&self, pool: PoolId);

    /// A reservation was fulfilled. A connection was available in time.
    fn reservation_fulfilled(
        &self,
        reservation_time: Duration,
        checkout_request_time: Duration,
        pool: PoolId,
    );

    /// A reservation was not fulfilled. A connection was mostly not available in time.
    fn reservation_not_fulfilled(
        &self,
        reservation_time: Duration,
        checkout_request_time: Duration,
        pool: PoolId,
    );

    /// The reservation queue has a limit and that limit was just reached.
    /// This means a checkout has instantaneously failed.
    fn reservation_limit_reached(&self, pool: PoolId);

    /// The connection factory was asked to create a new connection but it failed to do so.
    fn connection_factory_failed(&self, pool: PoolId);

    /// A pool internal message was received
    fn internal_message_received(&self, latency: Duration, pool: PoolId);

    fn checkout_message_received(&self, latency: Duration, pool: PoolId);

    fn relevant_message_processed(&self, processing_time: Duration, pool: PoolId);
}

#[derive(Clone)]
pub(crate) enum InstrumentationFlavour {
    NoInstrumentation,
    Custom(Arc<dyn Instrumentation + Sync + Send + 'static>),
    #[cfg(feature = "metrix")]
    Metrix(MetrixInstrumentation),
}

impl Instrumentation for InstrumentationFlavour {
    fn pool_added(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.pool_added(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.pool_added(pool),
        }
    }

    fn pool_removed(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.pool_removed(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.pool_removed(pool),
        }
    }

    fn checked_out_connection(
        &self,
        idle_for: Duration,
        time_since_checkout_request: Duration,
        pool: PoolId,
    ) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => {
                i.checked_out_connection(idle_for, time_since_checkout_request, pool)
            }
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => {
                i.checked_out_connection(idle_for, time_since_checkout_request, pool)
            }
        }
    }
    fn checked_in_returned_connection(&self, flight_time: Duration, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => {
                i.checked_in_returned_connection(flight_time, pool)
            }
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => {
                i.checked_in_returned_connection(flight_time, pool)
            }
        }
    }
    fn checked_in_new_connection(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.checked_in_new_connection(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.checked_in_new_connection(pool),
        }
    }
    fn connection_dropped(&self, flight_time: Option<Duration>, lifetime: Duration, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.connection_dropped(flight_time, lifetime, pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.connection_dropped(flight_time, lifetime, pool),
        }
    }
    fn connection_created(&self, connected_after: Duration, total_time: Duration, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => {
                i.connection_created(connected_after, total_time, pool)
            }
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => {
                i.connection_created(connected_after, total_time, pool)
            }
        }
    }
    fn idle_inc(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.idle_inc(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.idle_inc(pool),
        }
    }
    fn idle_dec(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.idle_dec(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.idle_dec(pool),
        }
    }
    fn in_flight_inc(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.in_flight_inc(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.in_flight_inc(pool),
        }
    }
    fn in_flight_dec(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.in_flight_dec(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.in_flight_dec(pool),
        }
    }
    fn reservation_added(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.reservation_added(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.reservation_added(pool),
        }
    }
    fn reservation_fulfilled(
        &self,
        reservation_time: Duration,
        checkout_request_time: Duration,
        pool: PoolId,
    ) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => {
                i.reservation_fulfilled(reservation_time, checkout_request_time, pool)
            }
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => {
                i.reservation_fulfilled(reservation_time, checkout_request_time, pool)
            }
        }
    }
    fn reservation_not_fulfilled(
        &self,
        reservation_time: Duration,
        checkout_request_time: Duration,
        pool: PoolId,
    ) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => {
                i.reservation_not_fulfilled(reservation_time, checkout_request_time, pool)
            }
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => {
                i.reservation_not_fulfilled(reservation_time, checkout_request_time, pool)
            }
        }
    }
    fn reservation_limit_reached(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.reservation_limit_reached(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.reservation_limit_reached(pool),
        }
    }
    fn connection_factory_failed(&self, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.connection_factory_failed(pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.connection_factory_failed(pool),
        }
    }

    fn internal_message_received(&self, latency: Duration, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.internal_message_received(latency, pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.internal_message_received(latency, pool),
        }
    }

    fn checkout_message_received(&self, latency: Duration, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => i.checkout_message_received(latency, pool),
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => i.checkout_message_received(latency, pool),
        }
    }

    fn relevant_message_processed(&self, processing_time: Duration, pool: PoolId) {
        match self {
            InstrumentationFlavour::NoInstrumentation => {}
            InstrumentationFlavour::Custom(i) => {
                i.relevant_message_processed(processing_time, pool)
            }
            #[cfg(feature = "metrix")]
            InstrumentationFlavour::Metrix(i) => {
                i.relevant_message_processed(processing_time, pool)
            }
        }
    }
}