1 |
use std::collections::HashMap;
|
2 |
|
3 |
use anyhow::Error;
|
4 |
use giterated_api::DaemonConnectionPool;
|
5 |
use giterated_models::model::instance::Instance;
|
6 |
|
7 |
#[derive(Default)]
|
8 |
pub struct InstanceConnections {
|
9 |
pools: HashMap<Instance, DaemonConnectionPool>,
|
10 |
}
|
11 |
|
12 |
impl InstanceConnections {
|
13 |
pub fn get_or_open(&mut self, instance: &Instance) -> Result<DaemonConnectionPool, Error> {
|
14 |
if let Some(pool) = self.pools.get(instance) {
|
15 |
Ok(pool.clone())
|
16 |
} else {
|
17 |
let pool = DaemonConnectionPool::connect(instance.clone()).unwrap();
|
18 |
self.pools.insert(instance.clone(), pool.clone());
|
19 |
|
20 |
Ok(pool)
|
21 |
}
|
22 |
}
|
23 |
}
|
24 |
|