|
| 1 | +/* |
| 2 | + * Copyright 2022, Sayan Nandan <[email protected]> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// re-exports |
| 18 | +#[cfg(any(feature = "sync", feature = "pool"))] |
| 19 | +pub use self::sync_impls::{ConnectionManager, Pool, TlsPool}; |
| 20 | + |
| 21 | +use crate::error::Error; |
| 22 | +use core::fmt; |
| 23 | + |
| 24 | +#[derive(Debug)] |
| 25 | +pub enum PoolError { |
| 26 | + PoolError(String), |
| 27 | + Other(Error), |
| 28 | +} |
| 29 | + |
| 30 | +impl From<Error> for PoolError { |
| 31 | + fn from(e: Error) -> Self { |
| 32 | + Self::Other(e) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl fmt::Display for PoolError { |
| 37 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 38 | + match self { |
| 39 | + Self::PoolError(e) => write!(f, "Pool error: {}", e), |
| 40 | + Self::Other(e) => write!(f, "Pool connection error: {}", e), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl std::error::Error for PoolError {} |
| 46 | + |
| 47 | +#[derive(Debug)] |
| 48 | +pub enum ConnectionProfile { |
| 49 | + NoTls { |
| 50 | + host: String, |
| 51 | + port: u16, |
| 52 | + }, |
| 53 | + Tls { |
| 54 | + host: String, |
| 55 | + port: u16, |
| 56 | + cert: String, |
| 57 | + }, |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(any(feature = "sync", feature = "pool"))] |
| 61 | +mod sync_impls { |
| 62 | + use super::{ConnectionProfile, PoolError}; |
| 63 | + use crate::sync::{Connection as SyncConnection, TlsConnection as SyncTlsConnection}; |
| 64 | + use crate::{ |
| 65 | + error::{Error, SkyhashError}, |
| 66 | + Element, Query, SkyQueryResult, SkyResult, |
| 67 | + }; |
| 68 | + use core::marker::PhantomData; |
| 69 | + use r2d2::ManageConnection; |
| 70 | + |
| 71 | + pub type Pool = r2d2::Pool<ConnectionManager<SyncConnection>>; |
| 72 | + pub type TlsPool = r2d2::Pool<ConnectionManager<SyncTlsConnection>>; |
| 73 | + |
| 74 | + #[derive(Debug)] |
| 75 | + pub struct ConnectionManager<C> { |
| 76 | + profile: ConnectionProfile, |
| 77 | + _m: PhantomData<C>, |
| 78 | + } |
| 79 | + |
| 80 | + impl<C> ConnectionManager<C> { |
| 81 | + fn _new(profile: ConnectionProfile) -> Self { |
| 82 | + Self { |
| 83 | + profile, |
| 84 | + _m: PhantomData, |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + impl ConnectionManager<SyncConnection> { |
| 90 | + pub fn new(host: String, port: u16) -> Self { |
| 91 | + Self::_new(ConnectionProfile::NoTls { host, port }) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + impl ConnectionManager<SyncTlsConnection> { |
| 96 | + pub fn new_tls(host: String, port: u16, cert: String) -> Self { |
| 97 | + Self::_new(ConnectionProfile::Tls { host, port, cert }) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + pub trait PoolableConnection: Send + Sync + Sized { |
| 102 | + fn get_connection(profile: &ConnectionProfile) -> SkyResult<Self>; |
| 103 | + fn run_query(&mut self, q: Query) -> SkyQueryResult; |
| 104 | + } |
| 105 | + |
| 106 | + impl PoolableConnection for SyncConnection { |
| 107 | + fn get_connection(profile: &ConnectionProfile) -> SkyResult<Self> { |
| 108 | + if let ConnectionProfile::NoTls { host, port } = profile { |
| 109 | + let c = Self::new(host, *port)?; |
| 110 | + Ok(c) |
| 111 | + } else { |
| 112 | + Err(Error::ConfigurationError( |
| 113 | + "Connection profile is TLS. Expected TCP", |
| 114 | + )) |
| 115 | + } |
| 116 | + } |
| 117 | + fn run_query(&mut self, q: Query) -> SkyQueryResult { |
| 118 | + self.run_simple_query(&q) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + impl PoolableConnection for SyncTlsConnection { |
| 123 | + fn get_connection(profile: &ConnectionProfile) -> SkyResult<Self> { |
| 124 | + if let ConnectionProfile::Tls { host, port, cert } = profile { |
| 125 | + let c = Self::new(host, *port, cert)?; |
| 126 | + Ok(c) |
| 127 | + } else { |
| 128 | + Err(Error::ConfigurationError( |
| 129 | + "Connection profile is TCP. Expected TLS", |
| 130 | + )) |
| 131 | + } |
| 132 | + } |
| 133 | + fn run_query(&mut self, q: Query) -> SkyQueryResult { |
| 134 | + self.run_simple_query(&q) |
| 135 | + } |
| 136 | + } |
| 137 | + impl<C: PoolableConnection + 'static> ManageConnection for ConnectionManager<C> { |
| 138 | + type Error = PoolError; |
| 139 | + type Connection = C; |
| 140 | + fn connect(&self) -> Result<Self::Connection, Self::Error> { |
| 141 | + C::get_connection(&self.profile).map_err(|e| Self::Error::Other(e)) |
| 142 | + } |
| 143 | + fn is_valid(&self, con: &mut Self::Connection) -> Result<(), Self::Error> { |
| 144 | + let q = crate::query!("HEYA"); |
| 145 | + match con.run_query(q)? { |
| 146 | + Element::String(st) if st.eq("HEY!") => Ok(()), |
| 147 | + _ => Err(PoolError::Other(Error::SkyError( |
| 148 | + SkyhashError::UnexpectedResponse, |
| 149 | + ))), |
| 150 | + } |
| 151 | + } |
| 152 | + fn has_broken(&self, _: &mut Self::Connection) -> bool { |
| 153 | + false |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments