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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! A generic connection pool, but async/await.
//!
//! Opening a new database connection every time one is needed is both
//! inefficient and can lead to resource exhaustion under high traffic
//! conditions. A connection pool maintains a set of open connections to a
//! database, handing them out for repeated use.
//!
//! mobc is agnostic to the connection type it is managing. Implementors of the
//! `ManageConnection` trait provide the database-specific logic to create and
//! check the health of connections.
//!
//! # Example
//!
//! Using an imaginary "foodb" database.
//!
//! ```rust,ignore
//! use tokio;
//!
//! extern crate mobc;
//! extern crate mobc_foodb;
//!
//! #[tokio::main]
//! async fn main() {
//!     let manager = mobc_foodb::FooConnectionManager::new("localhost:1234");
//!     let pool = mobc::Pool::builder()
//!         .max_size(15)
//!         .build(manager)
//!         .await
//!         .unwrap();
//!
//!     for _ in 0..20 {
//!         let pool = pool.clone();
//!         tokio::spawn(async {
//!             let conn = pool.get().await.unwrap();
//!             // use the connection
//!             // it will be returned to the pool when it falls out of scope.
//!         });
//!     }
//! }
//! ```

#![warn(missing_docs)]
mod config;
mod executor;

use config::Builder;
use config::Config;
pub use executor::Executor;
pub use futures;
use futures::channel::mpsc;
use futures::lock::{Mutex, MutexGuard};
use futures::Future;
use futures::FutureExt;
use futures::StreamExt;
use log::debug;
use std::error;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Weak};
use std::time::{Duration, Instant};
use tokio_timer::{delay, Interval};

static CONNECTION_ID: AtomicUsize = AtomicUsize::new(0);

/// The error type returned by methods in this crate.
pub enum Error<E> {
    /// Manager Errors
    Inner(E),
    /// Timeout
    Timeout,
}

impl<E> From<E> for Error<E> {
    fn from(e: E) -> Error<E> {
        Error::Inner(e)
    }
}

impl<E> fmt::Display for Error<E>
where
    E: error::Error + 'static,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Inner(ref err) => write!(f, "{}", err),
            Error::Timeout => write!(f, "Timed out in mobc"),
        }
    }
}

impl<E> fmt::Debug for Error<E>
where
    E: error::Error + 'static,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Inner(ref err) => write!(f, "{:?}", err),
            Error::Timeout => write!(f, "Timed out in mobc"),
        }
    }
}

/// Future alias
pub type AnyFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;

/// A trait which provides connection-specific functionality.
pub trait ConnectionManager: Send + Sync + 'static {
    /// The connection type this manager deals with.
    type Connection: Send + 'static;
    /// The error type returned by `Connection`s.
    type Error: error::Error + Send + Sync + 'static;
    /// The executor type this manager bases.
    type Executor: Executor;

    /// Get a future executor.
    fn get_executor(&self) -> Self::Executor;

    /// Attempts to create a new connection.
    fn connect(&self) -> AnyFuture<Self::Connection, Self::Error>;

    /// Determines if the connection is still connected to the database.
    ///
    /// A standard implementation would check if a simple query like `SELECT 1`
    /// succeeds.
    fn is_valid(&self, conn: Self::Connection) -> AnyFuture<Self::Connection, Self::Error>;
    /// *Quickly* determines if the connection is no longer usable.
    ///
    /// This will be called synchronously every time a connection is returned
    /// to the pool, so it should *not* block. If it returns `true`, the
    /// connection will be discarded.
    ///
    /// For example, an implementation might check if the underlying TCP socket
    /// has disconnected. Implementations that do not support this kind of
    /// fast health check may simply return `false`.
    fn has_broken(&self, conn: &mut Option<Self::Connection>) -> bool;
}

struct Conn<C> {
    raw: Option<C>,
    id: u64,
    birth: Instant,
}

struct IdleConn<C> {
    conn: Conn<C>,
    idle_start: Instant,
}

struct PoolInternals<C> {
    conns: mpsc::Sender<IdleConn<C>>,
    num_conns: u32,
    idle_conns: u32,
    pending_conns: u32,
    last_error: Option<String>,
    is_initial_done: bool,
    initial_done: mpsc::Sender<()>,
}

struct SharedPool<M>
where
    M: ConnectionManager,
{
    config: Config<M::Executor>,
    manager: M,
    internals: Mutex<PoolInternals<M::Connection>>,
    conns: Mutex<mpsc::Receiver<IdleConn<M::Connection>>>,
    initial_wg: Mutex<mpsc::Receiver<()>>,
}

/// A generic connection pool.
pub struct Pool<M>(Arc<SharedPool<M>>)
where
    M: ConnectionManager;

/// Returns a new `Pool` referencing the same state as `self`.
impl<M> Clone for Pool<M>
where
    M: ConnectionManager,
{
    fn clone(&self) -> Self {
        Pool(self.0.clone())
    }
}

impl<M> Pool<M>
where
    M: ConnectionManager,
{
    /// Creates a new connection pool with a default configuration.
    pub async fn new<E>(manager: M) -> Result<Pool<M>, Error<E>>
    where
        Error<E>: std::convert::From<<M as ConnectionManager>::Error>,
    {
        Pool::builder().build(manager).await
    }

    /// Returns a builder type to configure a new pool.
    pub fn builder() -> Builder<M> {
        Builder::new()
    }

    async fn new_inner(config: Config<M::Executor>, manager: M, reaper_rate: Duration) -> Pool<M> {
        let (recycle, conns) = mpsc::channel(config.max_size as usize);
        let initial_size = config.min_idle.unwrap_or(config.max_size);
        let (initial_done, initial_wg) = mpsc::channel(initial_size as usize);

        let internals = PoolInternals {
            conns: recycle,
            num_conns: 0,
            pending_conns: 0,
            idle_conns: 0,
            last_error: None,
            is_initial_done: false,
            initial_done,
        };

        let shared = Arc::new(SharedPool {
            config: config,
            manager: manager,
            internals: Mutex::new(internals),
            conns: Mutex::new(conns),
            initial_wg: Mutex::new(initial_wg),
        });

        let mut internals = shared.internals.lock().await;
        establish_idle_connections(&shared, &mut internals);

        if shared.config.max_lifetime.is_some() || shared.config.idle_timeout.is_some() {
            reap_connections(&shared, reaper_rate);
        }

        drop(internals);

        Pool(shared)
    }

    /// Retrieves a connection from the pool.
    ///
    /// Waits for at most the configured connection timeout before returning an
    /// error.
    pub async fn get<E>(&self) -> Result<PooledConnection<M>, Error<E>>
    where
        Error<E>: std::convert::From<<M as ConnectionManager>::Error>,
    {
        self.get_timeout(self.0.config.connection_timeout).await
    }

    /// Retrieves a connection from the pool, waiting for at most `timeout`
    ///
    /// The given timeout will be used instead of the configured connection
    /// timeout.
    pub async fn get_timeout<E>(&self, timeout: Duration) -> Result<PooledConnection<M>, Error<E>>
    where
        Error<E>: std::convert::From<<M as ConnectionManager>::Error>,
    {
        let start = Instant::now();
        let end = start + timeout;
        let timeout = delay(end);

        debug!("try to lock the conns");
        let mut conns = self.0.conns.lock().await;
        debug!("waiting for get timeout");
        futures::select! {
            () = timeout.fuse() => Err(Error::Timeout),
            r = conns.next() => match r {
                Some(conn) => {
                    drop(conns);
                    debug!("get conn");
                    let mut internals = self.0.internals.lock().await;
                    internals.idle_conns -= 1;
                    establish_idle_connections(&self.0, &mut internals);
                    return Ok(PooledConnection {
                        pool: Some(self.clone()),
                        conn: Some(conn.conn),
                    })
                }
                None => Err(Error::Timeout),
            }
        }
    }

    /// Attempts to retrieve a connection from the pool if there is one
    /// available.
    ///
    /// Returns `None` if there are no idle connections available in the pool.
    /// This method will not block waiting to establish a new connection.
    pub async fn try_get(&self) -> Option<PooledConnection<M>> {
        let mut conns = self.0.conns.lock().await;
        match conns.try_next() {
            Ok(Some(conn)) => {
                let mut internals = self.0.internals.lock().await;
                internals.idle_conns -= 1;
                Some(PooledConnection {
                    pool: Some(self.clone()),
                    conn: Some(conn.conn),
                })
            }
            _ => None,
        }
    }

    async fn wait_for_initialization<E>(&self) -> Result<(), Error<E>>
    where
        Error<E>: std::convert::From<<M as ConnectionManager>::Error>,
    {
        debug!("waiting for initialization");
        let end = Instant::now() + self.0.config.connection_timeout;
        let mut timeout = delay(end).fuse();
        let initial_size = self.0.config.min_idle.unwrap_or(self.0.config.max_size);
        let mut initial_wg = self.0.initial_wg.lock().await;

        loop {
            futures::select! {
                () = timeout => return Err(Error::Timeout),
                _ = initial_wg.next() => {
                    let mut internals = self.0.internals.lock().await;
                    if internals.num_conns == initial_size {
                        internals.is_initial_done = true;
                        break;
                    }
                }

            }
        }

        Ok(())
    }

    fn put_back(self, _checkout: Instant, mut conn: Conn<M::Connection>) {
        // let new_shared = Arc::downgrade(self);

        let _ = self.0.config.executor.clone().spawn(Box::pin(async move {
            // This is specified to be fast, but call it before locking anyways
            let broken = conn.raw.is_none() || self.0.manager.has_broken(&mut conn.raw);

            let mut internals = self.0.internals.lock().await;
            if broken {
                drop_conns(&self.0, internals, vec![conn]);
                return;
            } else {
                let conn = IdleConn {
                    conn,
                    idle_start: Instant::now(),
                };
                debug!("put back");
                internals.conns.try_send(conn).unwrap();
                internals.idle_conns += 1;
            }
        }));
    }

    /// Returns information about the current state of the pool.
    pub async fn state(&self) -> State {
        let internals = self.0.internals.lock().await;
        State {
            connections: internals.num_conns,
            idle_connections: internals.idle_conns,
            _p: (),
        }
    }
}

fn drop_conns<M>(
    shared: &Arc<SharedPool<M>>,
    mut internals: MutexGuard<PoolInternals<M::Connection>>,
    conn: Vec<Conn<M::Connection>>,
) where
    M: ConnectionManager,
{
    internals.num_conns -= conn.len() as u32;
    establish_idle_connections(shared, &mut internals);
    drop(internals);
}

fn establish_idle_connections<M>(
    shared: &Arc<SharedPool<M>>,
    internals: &mut PoolInternals<M::Connection>,
) where
    M: ConnectionManager,
{
    let min = shared.config.min_idle.unwrap_or(shared.config.max_size);
    let idle = internals.idle_conns as u32;
    debug!(
        "idle {} min {}, {}, {}",
        idle, min, internals.num_conns, internals.pending_conns,
    );
    for _ in idle..min {
        add_connection(shared, internals);
    }
}

fn add_connection<M>(shared: &Arc<SharedPool<M>>, internals: &mut PoolInternals<M::Connection>)
where
    M: ConnectionManager,
{
    debug!(
        "num_conns {}, pending_conns {}, max_size {}",
        internals.num_conns, internals.pending_conns, shared.config.max_size
    );
    if internals.num_conns + internals.pending_conns >= shared.config.max_size {
        return;
    }
    debug!("add connection");

    internals.pending_conns += 1;
    inner(Duration::from_secs(0), shared);

    fn inner<M>(_delay: Duration, shared: &Arc<SharedPool<M>>)
    where
        M: ConnectionManager,
    {
        debug!("inner add connection");
        let new_shared = Arc::downgrade(shared);
        let _ = shared.config.executor.clone().spawn(Box::pin(async move {
            let shared = match new_shared.upgrade() {
                Some(shared) => shared,
                None => return,
            };

            let conn = shared.manager.connect().await;
            match conn {
                Ok(conn) => {
                    debug!("adding connection");
                    let id = CONNECTION_ID.fetch_add(1, Ordering::Relaxed) as u64;
                    let mut internals = shared.internals.lock().await;

                    internals.last_error = None;
                    let now = Instant::now();
                    let mut conn = IdleConn {
                        conn: Conn {
                            raw: Some(conn),
                            birth: now,
                            id,
                        },
                        idle_start: now,
                    };

                    loop {
                        match internals.conns.try_send(conn) {
                            Ok(()) => break,
                            Err(c) => conn = c.into_inner(),
                        }
                    }
                    internals.pending_conns -= 1;
                    internals.idle_conns += 1;
                    internals.num_conns += 1;
                    if !internals.is_initial_done {
                        internals.initial_done.try_send(()).unwrap();
                    }
                    drop(internals);
                }
                Err(err) => {
                    shared.internals.lock().await.last_error = Some(err.to_string());
                    let delay = Duration::from_millis(200);
                    inner(delay, &shared);
                }
            }
        }));
    }
}

fn reap_connections<M>(shared: &Arc<SharedPool<M>>, reaper_rate: Duration)
where
    M: ConnectionManager,
{
    let new_shared = Arc::downgrade(shared);
    let _ = shared
        .manager
        .get_executor()
        .clone()
        .spawn(Box::pin(async move {
            while let Some(_) = Interval::new_interval(reaper_rate).next().await {
                debug!("start reaping");
                reap_conn(&new_shared).await;
            }
            debug!("stop reaping connections");
        }));

    async fn reap_conn<M>(shared: &Weak<SharedPool<M>>)
    where
        M: ConnectionManager,
    {
        let shared = match shared.upgrade() {
            Some(shared) => shared,
            None => return,
        };

        let mut to_drop = vec![];

        let mut internals = shared.internals.lock().await;
        let mut conns = shared.conns.lock().await;
        let mut checked_num: u32 = 0;

        let now = Instant::now();
        while let Ok(Some(conn)) = conns.try_next() {
            let mut reap = false;
            if let Some(timeout) = shared.config.idle_timeout {
                debug!("idle time {:?}", now - conn.idle_start);
                reap |= now - conn.idle_start >= timeout;
            }
            if let Some(lifetime) = shared.config.max_lifetime {
                reap |= now - conn.conn.birth >= lifetime;
            }
            debug!("reap => {}", reap);
            if reap {
                to_drop.push(conn.conn);
            } else {
                internals.conns.try_send(conn).unwrap()
            }

            checked_num += 1;
            if checked_num == internals.idle_conns {
                break;
            }
        }

        debug!("no more conns");
        internals.idle_conns -= to_drop.len() as u32;
        drop_conns(&shared, internals, to_drop);
        debug!("reap finish");
    }
}

/// Information about the state of a `Pool`.
pub struct State {
    /// The number of connections currently being managed by the pool.
    pub connections: u32,
    /// The number of idle connections.
    pub idle_connections: u32,
    _p: (),
}

impl fmt::Debug for State {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("State")
            .field("connections", &self.connections)
            .field("idle_connections", &self.idle_connections)
            .finish()
    }
}
/// A smart pointer wrapping a connection.
pub struct PooledConnection<M>
where
    M: ConnectionManager,
{
    pool: Option<Pool<M>>,
    conn: Option<Conn<M::Connection>>,
}

impl<M> PooledConnection<M>
where
    M: ConnectionManager,
{
    /// Takes the raw database connection
    pub fn take_raw_conn(&mut self) -> M::Connection {
        self.conn.as_mut().unwrap().raw.take().unwrap()
    }

    /// Put back the raw database connection
    pub fn set_raw_conn(&mut self, raw: M::Connection) {
        self.conn.as_mut().unwrap().raw = Some(raw);
    }
}

impl<M> Drop for PooledConnection<M>
where
    M: ConnectionManager,
{
    fn drop(&mut self) {
        self.pool
            .take()
            .unwrap()
            .put_back(Instant::now(), self.conn.take().unwrap());
    }
}

impl<M> Deref for PooledConnection<M>
where
    M: ConnectionManager,
{
    type Target = M::Connection;
    fn deref(&self) -> &Self::Target {
        &self.conn.as_ref().unwrap().raw.as_ref().unwrap()
    }
}

impl<M> DerefMut for PooledConnection<M>
where
    M: ConnectionManager,
{
    fn deref_mut(&mut self) -> &mut M::Connection {
        self.conn.as_mut().unwrap().raw.as_mut().unwrap()
    }
}