Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

PooledObject timeout #1

Closed
brunodoamaral opened this issue Oct 31, 2016 · 2 comments
Closed

PooledObject timeout #1

brunodoamaral opened this issue Oct 31, 2016 · 2 comments
Assignees

Comments

@brunodoamaral
Copy link

Hi! First, thanks for your lib!

I'd like to make a suggestion: where can we implement a timeout feature in order to release an unused PooledObject from the pool?

It should be nice to have a constructor like:

public ObjectPool(int minimumPoolSize, int maximumPoolSize, long lazyObjectsTimeoutMs, Func<T> factoryMethod)

What do you think?

@pomma89
Copy link
Owner

pomma89 commented Nov 1, 2016

Hi @brunodoamaral,

Thank you for your suggestion. I think that what you ask could be implemented in two different ways:

  1. Create a timer which periodically removes objects that do not match a specified filter (in your case, time itself is the filter).
  2. Do not let in/out of the pool all objects which do not match a specified filter (as before, time would be your filter).

Solution 1 is the only one which would ensure consistent "freshness" of objects contained in the pool. However, this would imply adding an additional thread whose job is cleaning up the pool, and, well, I do not really like the idea of shipping a lib which executes "hidden" work on separate threads. Thus, it will not be implemented, I'm sorry.

Solution 2 works perfectly when the pool is frequently used, because:

  • When an object is going out of the pool, it might get discarded if it does not respect the filter. It would not be returned to the user and removed from the pool.
  • When an object is returning to the pool, it might get discarded if it does not respect the filter.

However, if the pool is not used at all, objects not respecting the filter would stay in pool anyway. In any case, when one chooses to use an object pool, the number of pool usages is expected to be very high, so we should not have an issue there. In order to implement solution 2, we need a filter for items going out of the pool (not implemented as of now), and one for items going in (already available and actually used for specialized pools).

If solution 2 is OK for you, then I will start implementing it as soon as possible.

@pomma89
Copy link
Owner

pomma89 commented Apr 8, 2017

Hi, just released v3 implements "Solution 1" with a new class, TimedObjectPool, which extends ObjectPool and features a customizable timeout.

@pomma89 pomma89 closed this as completed Apr 8, 2017
@pomma89 pomma89 self-assigned this Aug 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants