1use std::time::Duration;
2
3use deadpool_redis::{Config as RedisConfig, Pool, Runtime};
4
5use crate::error::RedisResult;
6
7pub struct PoolConfig {
9 pub url: String,
11 pub max_size: usize,
13 pub wait_timeout: Duration,
15 pub create_timeout: Duration,
17 pub recycle_timeout: Duration,
19}
20
21impl PoolConfig {
22 pub fn new(url: impl Into<String>) -> Self {
24 Self {
25 url: url.into(),
26 max_size: 16,
27 wait_timeout: Duration::from_secs(2),
28 create_timeout: Duration::from_secs(2),
29 recycle_timeout: Duration::from_secs(2),
30 }
31 }
32
33 pub fn max_size(mut self, size: usize) -> Self {
34 self.max_size = size;
35 self
36 }
37
38 pub fn wait_timeout(mut self, timeout: Duration) -> Self {
39 self.wait_timeout = timeout;
40 self
41 }
42
43 pub fn create_timeout(mut self, timeout: Duration) -> Self {
44 self.create_timeout = timeout;
45 self
46 }
47
48 pub fn recycle_timeout(mut self, timeout: Duration) -> Self {
49 self.recycle_timeout = timeout;
50 self
51 }
52
53 pub fn build(self) -> RedisResult<Pool> {
55 let cfg = RedisConfig::from_url(&self.url);
56 Ok(cfg
57 .builder()?
58 .max_size(self.max_size)
59 .wait_timeout(Some(self.wait_timeout))
60 .create_timeout(Some(self.create_timeout))
61 .recycle_timeout(Some(self.recycle_timeout))
62 .runtime(Runtime::Tokio1)
63 .build()?)
64 }
65}