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
//! # Reool
//!
//! Currently in early development.
//!
//! ## About
//!
//! Reool is a connection pool for Redis based on [redis-rs](https://crates.io/crates/redis).
//!
//! Currently `reool` is a fixed size connection pool.
//! `Reool` provides an interface for instrumentation.
//!
//!
//! You should also consider multiplexing instead of a pool based on your needs.
//!
//! The `PooledConnection` of `reool` implements the `ConnectionLike`
//! interface of [redis-rs](https://crates.io/crates/redis) for easier integration.
//!
//! For documentation visit [crates.io](https://crates.io/crates/reool).
//!
//! ## License
//!
//! Reool is distributed under the terms of both the MIT license and the
//! Apache License (Version 2.0).
//!
//! See LICENSE-APACHE and LICENSE-MIT for details.
//! License: Apache-2.0/MIT
use std::time::Duration;

use futures::{future::Future, try_ready, Async, Poll};
use redis::{r#async::Connection, Client};

use crate::error::ReoolError;
use crate::pool::{
    Checkout as PoolCheckout, ConnectionFactory, ConnectionFactoryFuture, NewConnectionError,
    Poolable,
};

mod backoff_strategy;
mod commands;
mod error;
pub(crate) mod executor_flavour;
pub(crate) mod helpers;
pub mod instrumentation;
pub mod multi_node_pool;
pub mod node_pool;
mod pool;
mod pooled_connection;

pub use commands::*;
pub use pooled_connection::PooledConnection;

/// A `Future` that represents a checkout.
///
/// A `Checkout` can fail for various reasons.
///
/// The most common ones are:
/// * There was a timeout on the checkout and it timed out
/// * The queue size was limited and the limit was reached
/// * There are simply no connections available
pub struct Checkout(PoolCheckout<Connection>);

impl Future for Checkout {
    type Item = PooledConnection;
    type Error = ReoolError;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let managed = try_ready!(self.0.poll());
        Ok(Async::Ready(PooledConnection {
            managed,
            last_op_completed: true,
        }))
    }
}

/// A trait that can be used as an interface for a Redis pool.
pub trait RedisPool {
    /// Checkout a new connection and if the request has to be enqueued
    /// use a timeout as defined by the implementor.
    fn check_out(&self) -> Checkout;
    /// Checkout a new connection and if the request has to be enqueued
    /// use the given timeout or wait indefinetly.
    fn check_out_explicit_timeout(&self, timeout: Option<Duration>) -> Checkout;
}

impl Poolable for Connection {}

impl ConnectionFactory for Client {
    type Connection = Connection;

    fn create_connection(&self) -> ConnectionFactoryFuture<Self::Connection> {
        Box::new(self.get_async_connection().map_err(NewConnectionError::new))
    }
}