Skip to content

Commit aa636bf

Browse files
committed
refactor: more robust give back to Resource Pool implementation
The 'take' function of the 'ResourcePoolItem' is now private, and a new 'give_back_resource_pool_item' has been added to the 'ResourcePool'.
1 parent 29b7c45 commit aa636bf

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

mithril-aggregator/src/services/prover.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ impl ProverService for MithrilProverService {
148148

149149
// 5 - Compute the proof for all transactions
150150
if let Ok(mk_proof) = mk_map.compute_proof(transaction_hashes) {
151-
if let Some(resource) = mk_map.take() {
152-
self.mk_map_pool
153-
.give_back_resource(resource, mk_map.discriminant())?;
154-
}
155-
151+
self.mk_map_pool.give_back_resource_pool_item(mk_map)?;
156152
let transaction_hashes_certified: Vec<TransactionHash> = transaction_hashes
157153
.iter()
158154
.filter(|hash| mk_proof.contains(&hash.as_str().into()).is_ok())

mithril-common/src/resource_pool.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ impl<T: Send + Sync> ResourcePool<T> {
9595
Ok(())
9696
}
9797

98+
/// Give back a resource pool item to the pool
99+
/// If the resource pool item has not been taken yet, the resource will be given back
100+
/// If the resource pool item has been taken, nothing will happen
101+
pub fn give_back_resource_pool_item(
102+
&self,
103+
resource_pool_item: ResourcePoolItem<'_, T>,
104+
) -> StdResult<()> {
105+
let mut resource_pool_item = resource_pool_item;
106+
resource_pool_item
107+
.take()
108+
.map(|resource_item| self.give_back_resource(resource_item, self.discriminant()?));
109+
110+
Ok(())
111+
}
112+
98113
/// Clear the pool
99114
pub fn clear(&self) {
100115
let mut resources = self.resources.lock().unwrap();
@@ -168,7 +183,7 @@ impl<'a, T: Send + Sync> ResourcePoolItem<'a, T> {
168183
}
169184

170185
/// Take the inner resource if exists
171-
pub fn take(&mut self) -> Option<T> {
186+
fn take(&mut self) -> Option<T> {
172187
self.resource.take()
173188
}
174189
}
@@ -311,4 +326,36 @@ mod tests {
311326

312327
assert_eq!(pool.count().unwrap(), pool_size - 1);
313328
}
329+
330+
#[tokio::test]
331+
async fn test_resource_pool_gives_back_fresh_resource_pool_item_if_not_taken_yet() {
332+
let pool_size = 10;
333+
let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
334+
let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
335+
assert_eq!(pool.count().unwrap(), pool_size);
336+
337+
let resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
338+
assert_eq!(pool.count().unwrap(), pool_size - 1);
339+
pool.give_back_resource_pool_item(resource_item).unwrap();
340+
341+
assert_eq!(pool.count().unwrap(), pool_size);
342+
}
343+
344+
#[tokio::test]
345+
async fn test_resource_pool_does_not_give_back_fresh_resource_pool_item_if_already_taken() {
346+
let pool_size = 10;
347+
let resources_expected: Vec<String> = (0..pool_size).map(|i| i.to_string()).collect();
348+
let pool = ResourcePool::<String>::new(pool_size, resources_expected.clone());
349+
assert_eq!(pool.count().unwrap(), pool_size);
350+
351+
{
352+
// Resource taken will be dropped when exiting this block scope
353+
let mut resource_item = pool.acquire_resource(Duration::from_millis(10)).unwrap();
354+
assert_eq!(pool.count().unwrap(), pool_size - 1);
355+
let _resource = resource_item.take().unwrap(); // This can only happen in this module as 'take' is private
356+
pool.give_back_resource_pool_item(resource_item).unwrap();
357+
}
358+
359+
assert_eq!(pool.count().unwrap(), pool_size - 1);
360+
}
314361
}

0 commit comments

Comments
 (0)