-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: return the current number of connections in pool from retain
#370
Comments
retain
retain
I wonder if we might want a struct RetainResult<T> {
pub retained: usize,
pub removed: Vec<T>,
} Maybe someone wants to remove a couple of objects from the pool and still do something meaningful with them before finally dropping them for good? btw. keep in mind that the number of retained objects is NOT the current pool size. Objects currently borrowed from the pool are completely invisible to this method. |
Great point! We're just looking to see if our pools are too large/small by checking the idle timeout. Currently borrowed objects shouldn't hit that.
|
Well, or would it be easiest to change the |
You are right. Changing this to a I do like the idea with the Now that I think about it it could even improve the performance a bit as the |
I started looking into the pub(crate) async fn apply(
&self,
inner: &mut ObjectInner<M>,
) -> Result<(), HookError<M::Error>> {
for hook in &self.vec {
match hook {
Hook::Fn(f) => f(&mut inner.obj, &inner.metrics)?,
Hook::AsyncFn(f) => f(&mut inner.obj, &inner.metrics).await?,
};
}
Ok(())
} That means that we need mutable access to One workaround I was thinking was that we could use a Though honestly, the more I think about it, the worse it is to potentially block on every create/recycle due to a hook. Curious what your opinion is, @bikeshedder (I've been meaning to look at |
I just gave this a try myself and changing the method signature does seam to do the the trick: -pub fn retain(&self, f: impl Fn(&M::Type, Metrics) -> bool) {
+pub fn retain(&self, mut f: impl FnMut(&M::Type, Metrics) -> bool) { Were you trying something different? |
I have merged both the As this is not a breaking change - unless someone explicitly expected the |
Thanks! |
I'd like to track how many connections are currently in a given
Pool
so that I can gather some data and adjust the pool size. I'm pruning connections based on an idle timeout, soretain
is perfect here. However, it takes anFn
, which prevents mutable state borrows. This means I can't do something like:The one way I can think of doing this is having
removed
be anAtomicU64
, since that gives us access tofetch_add
which takes an immutable receiver. That opens us up to unnecessary overhead though, what with all the atomic operations in an already-blocking closure.It seems like the calculation is already done, we just have to return
guard.size
fromretain()
:I'm happy to raise a PR, or if there are any workarounds, please let me know!
The text was updated successfully, but these errors were encountered: