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
//! Resource pool implementation

use anyhow::Context;
use std::{
    collections::VecDeque,
    ops::{Deref, DerefMut},
    sync::{Condvar, Mutex},
    time::Duration,
};
use thiserror::Error;

use crate::StdResult;

/// [ResourcePool] related errors.
#[derive(Error, Debug)]
pub enum ResourcePoolError {
    /// Internal Mutex is poisoned
    #[error("Poisoned mutex caused error during acquire lock on resource pool")]
    PoisonedLock,

    /// Acquire resource has timed out
    #[error("Acquire resource has timed out")]
    AcquireTimeout,
}

/// Resource pool implementation (FIFO)
pub struct ResourcePool<T: Reset + Send + Sync> {
    /// The size of the pool
    size: usize,

    /// Discriminant for the resource pool to check if a returned resource is stale
    discriminant: Mutex<u64>,

    /// Resources in the pool
    resources: Mutex<VecDeque<T>>,

    /// Condition variable to notify when a resource is available
    not_empty: Condvar,
}

impl<T: Reset + Send + Sync> ResourcePool<T> {
    /// Create a new resource pool
    pub fn new(pool_size: usize, resources: Vec<T>) -> Self {
        Self {
            size: pool_size,
            discriminant: Mutex::new(0),
            resources: Mutex::new(resources.into()),
            not_empty: Condvar::new(),
        }
    }

    /// Acquire a resource from the pool with a timeout
    pub fn acquire_resource(&self, timeout: Duration) -> StdResult<ResourcePoolItem<'_, T>> {
        let mut resources = self
            .resources
            .lock()
            .map_err(|_| ResourcePoolError::PoisonedLock)
            .with_context(|| "Resource pool 'acquire_resource' failed locking Mutex")?;
        while resources.is_empty() {
            let (resources_locked, wait_result) = self
                .not_empty
                .wait_timeout(resources, timeout)
                .map_err(|_| ResourcePoolError::PoisonedLock)
                .with_context(|| "Resource pool 'acquire_resource' failed waiting for resource")?;
            if wait_result.timed_out() {
                return Err(ResourcePoolError::AcquireTimeout)
                    .with_context(|| "Resource pool 'acquire_resource' has timed out");
            }
            resources = resources_locked;
        }

        Ok(ResourcePoolItem::new(self, resources.pop_front().unwrap()))
    }

    /// Give back a resource to the pool
    /// A resource is given back to the pool only if the discriminant matches
    /// and if the pool is not already full
    pub fn give_back_resource(&self, resource: T, discriminant: u64) -> StdResult<()> {
        let mut resource = resource;
        resource.reset()?;
        if self.count()? == self.size {
            // Pool is full
            return Ok(());
        }
        let mut resources = self
            .resources
            .lock()
            .map_err(|_| ResourcePoolError::PoisonedLock)
            .with_context(|| "Resource pool 'give_back_resource' failed locking Mutex")?;
        if self.discriminant()? != discriminant {
            // Stale resource
            return Ok(());
        }
        resources.push_back(resource);
        self.not_empty.notify_one();

        Ok(())
    }

    /// Give back a resource pool item to the pool
    /// If the resource pool item has not been taken yet, the resource will be given back
    /// If the resource pool item has been taken, nothing will happen
    pub fn give_back_resource_pool_item(
        &self,
        resource_pool_item: ResourcePoolItem<'_, T>,
    ) -> StdResult<()> {
        let mut resource_pool_item = resource_pool_item;
        resource_pool_item
            .take()
            .map(|resource_item| self.give_back_resource(resource_item, self.discriminant()?));

        Ok(())
    }

    /// Clear the pool
    pub fn clear(&self) {
        let mut resources = self.resources.lock().unwrap();
        resources.clear();
    }

    /// Get the discriminant of the resource pool item
    pub fn discriminant(&self) -> StdResult<u64> {
        Ok(*self
            .discriminant
            .lock()
            .map_err(|_| ResourcePoolError::PoisonedLock)
            .with_context(|| "Resource pool 'discriminant' failed locking Mutex")?)
    }

    /// Set the discriminant of the resource pool item
    pub fn set_discriminant(&self, discriminant: u64) -> StdResult<()> {
        let mut discriminant_guard = self
            .discriminant
            .lock()
            .map_err(|_| ResourcePoolError::PoisonedLock)
            .with_context(|| "Resource pool 'set_discriminant' failed locking Mutex")?;
        *discriminant_guard = discriminant;

        Ok(())
    }

    /// Count the resources in the pool
    pub fn count(&self) -> StdResult<usize> {
        Ok(self
            .resources
            .lock()
            .map_err(|_| ResourcePoolError::PoisonedLock)
            .with_context(|| "Resource pool 'count' failed locking Mutex")?
            .len())
    }

    /// Size of the resource pool
    pub fn size(&self) -> usize {
        self.size
    }
}

/// Resource pool item which will return the resource to the pool when dropped
pub struct ResourcePoolItem<'a, T: Reset + Send + Sync> {
    resource_pool: &'a ResourcePool<T>,
    discriminant: u64,
    resource: Option<T>,
}

impl<'a, T: Reset + Send + Sync> ResourcePoolItem<'a, T> {
    /// Create a new resource pool item
    pub fn new(resource_pool: &'a ResourcePool<T>, resource: T) -> Self {
        let discriminant = *resource_pool.discriminant.lock().unwrap();
        Self {
            resource_pool,
            discriminant,
            resource: Some(resource),
        }
    }

    /// Get the discriminant of the resource pool item
    pub fn discriminant(&self) -> u64 {
        self.discriminant
    }

    /// Take the inner resource if exists
    fn take(&mut self) -> Option<T> {
        self.resource.take()
    }
}

impl<T: Reset + Send + Sync> Deref for ResourcePoolItem<'_, T> {
    type Target = T;

    fn deref(&self) -> &T {
        self.resource.as_ref().unwrap()
    }
}

impl<T: Reset + Send + Sync> DerefMut for ResourcePoolItem<'_, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.resource.as_mut().unwrap()
    }
}

impl<T: Reset + Send + Sync> Drop for ResourcePoolItem<'_, T> {
    fn drop(&mut self) {
        self.take().map(|resource| {
            self.resource_pool
                .give_back_resource(resource, self.discriminant)
        });
    }
}

/// Reset trait which is implemented by pooled resource items.
/// As pool resource items are mutable, this will guarantee that the pool stays  consistent
/// and that acquired resource items do not depend from previous computations.
pub trait Reset {
    /// Reset the resource
    fn reset(&mut self) -> StdResult<()> {
        Ok(())
    }
}

cfg_test_tools! {
    impl Reset for String {}
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;

    /// Resource for testing reset
    #[derive(Default)]
    struct TestResetResource {
        reset: bool,
    }

    impl Reset for TestResetResource {
        fn reset(&mut self) -> StdResult<()> {
            self.reset = true;

            Ok(())
        }
    }

    #[test]
    fn test_resource_pool_acquire_returns_resource_when_available() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());

        let mut resources_items = vec![];
        for _ in 0..pool_size {
            let resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
            resources_items.push(resource_item);
        }
        let resources_result = resources_items
            .iter_mut()
            .map(|resource_item| resource_item.take().unwrap())
            .collect::<Vec<_>>();

        assert_eq!(resources_expected, resources_result);
        assert_eq!(pool.count().unwrap(), 0);
    }

    #[tokio::test]
    async fn test_resource_pool_acquire_locks_until_timeout_when_no_resource_available() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());

        let mut resources_items = vec![];
        for _ in 0..pool_size {
            let resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
            // The resource is stored in a list to avoid it being dropped when exiting this block scope
            resources_items.push(resource_item);
        }

        assert!(pool.acquire_resource(Duration::from_millis(1000)).is_err());
    }

    #[tokio::test]
    async fn test_resource_pool_clears_successfully() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
        assert_eq!(pool.count().unwrap(), pool_size);

        pool.clear();

        assert_eq!(pool.count().unwrap(), 0);
    }

    #[tokio::test]
    async fn test_resource_pool_gives_back_fresh_resource() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
        assert_eq!(pool.count().unwrap(), pool_size);

        let mut resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
        assert_eq!(pool.count().unwrap(), pool_size - 1);
        pool.give_back_resource(resource_item.take().unwrap(), pool.discriminant().unwrap())
            .unwrap();

        assert_eq!(pool.count().unwrap(), pool_size);
    }

    #[tokio::test]
    async fn test_resource_pool_gives_back_resource_automatically() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
        assert_eq!(pool.count().unwrap(), pool_size);

        {
            // Resource will be returned when resource item is dropped (will occur when exiting this block scope)
            let _resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
            assert_eq!(pool.count().unwrap(), pool_size - 1);
        }

        assert_eq!(pool.count().unwrap(), pool_size);
    }

    #[tokio::test]
    async fn test_resource_pool_does_not_give_back_resource_when_pool_is_full() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
        assert_eq!(pool.count().unwrap(), pool_size);

        pool.give_back_resource("resource".to_string(), pool.discriminant().unwrap())
            .unwrap();

        assert_eq!(pool.count().unwrap(), pool_size);
    }

    #[tokio::test]
    async fn test_resource_pool_does_not_give_back_stale_resource() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
        assert_eq!(pool.count().unwrap(), pool_size);

        let mut resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
        assert_eq!(pool.count().unwrap(), pool_size - 1);
        let discriminant_stale = pool.discriminant().unwrap();
        pool.set_discriminant(pool.discriminant().unwrap() + 1)
            .unwrap();
        pool.give_back_resource(resource_item.take().unwrap(), discriminant_stale)
            .unwrap();

        assert_eq!(pool.count().unwrap(), pool_size - 1);
    }

    #[tokio::test]
    async fn test_resource_pool_gives_back_fresh_resource_pool_item_if_not_taken_yet() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
        assert_eq!(pool.count().unwrap(), pool_size);

        let resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
        assert_eq!(pool.count().unwrap(), pool_size - 1);
        pool.give_back_resource_pool_item(resource_item).unwrap();

        assert_eq!(pool.count().unwrap(), pool_size);
    }

    #[tokio::test]
    async fn test_resource_pool_does_not_give_back_fresh_resource_pool_item_if_already_taken() {
        let pool_size = 10;
        let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
        let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
        assert_eq!(pool.count().unwrap(), pool_size);

        {
            // Resource taken will be dropped when exiting this block scope
            let mut resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
            assert_eq!(pool.count().unwrap(), pool_size - 1);
            let _resource = resource_item.take().unwrap(); // This can only happen in this module as 'take' is private
            pool.give_back_resource_pool_item(resource_item).unwrap();
        }

        assert_eq!(pool.count().unwrap(), pool_size - 1);
    }

    #[tokio::test]
    async fn test_resource_pool_is_reset_when_given_back() {
        let pool = ResourcePool::<TestResetResource>::new(1, vec![TestResetResource::default()]);

        let mut resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
        let resource = resource_item.take().unwrap();
        pool.give_back_resource(resource, pool.discriminant().unwrap())
            .unwrap();

        // Acquire the resource again and make sure it has been reseted
        let resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
        assert!(resource_item.reset);
    }

    #[tokio::test]
    async fn test_resource_pool_item_is_reset_when_given_back() {
        let pool = ResourcePool::<TestResetResource>::new(1, vec![TestResetResource::default()]);

        let resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
        pool.give_back_resource_pool_item(resource_item).unwrap();

        // Acquire the resource again and make sure it has been reseted
        let resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
        assert!(resource_item.reset);
    }
}