Skip to content
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

Implement Manager on a different type than Object #335

Closed
JosiahParry opened this issue Jun 18, 2024 · 3 comments
Closed

Implement Manager on a different type than Object #335

JosiahParry opened this issue Jun 18, 2024 · 3 comments
Labels
A-core Area: Core / deadpool invalid The issue is invalid and has been raised in error.

Comments

@JosiahParry
Copy link

I have a custom struct, for example we can call it App which has a field of Vec<Backend>. I would like implement the Manager trait for my App struct but have the impl Manager return a Backend that way I can track and manage other important state in the App struct.

I was able to implement Manager for App and specify type Type = Backend but .get() returned App not Backend ignoring the Type specification.

@bikeshedder
Copy link
Owner

bikeshedder commented Jun 18, 2024

I guess you stumbled over Object::<Backend> thinking that it does wrap the Backend. It does in fact wrap the Backend::Type.

Pool::<Backend>::get() returns W (= Object<Backend>):

pub struct Pool<M: Manager, W: From<Object<M>> = Object<M>> {

deadpool/src/managed/mod.rs

Lines 311 to 313 in 7b933c5

pub async fn get(&self) -> Result<W, PoolError<M::Error>> {
self.timeout_get(&self.timeouts()).await
}

Object<Backend> implements Deref<Target=Backend::Type>:

deadpool/src/managed/mod.rs

Lines 220 to 225 in 7b933c5

impl<M: Manager> Deref for Object<M> {
type Target = M::Type;
fn deref(&self) -> &M::Type {
&self.inner.as_ref().unwrap().obj
}
}

I guess you stumbled over Object::<Backend> thinking that it does wrap the Backend. It does in fact wrap the Backend::Type.


Regarding the W type. It's there so one can implement traits on objects returned by the pool. e.g. deadpool-redis uses this to implement the ConnectionLike trait:

/// Wrapper around [`redis::aio::MultiplexedConnection`].
///
/// This structure implements [`redis::aio::ConnectionLike`] and can therefore
/// be used just like a regular [`redis::aio::MultiplexedConnection`].
#[allow(missing_debug_implementations)] // `redis::aio::MultiplexedConnection: !Debug`
pub struct Connection {
conn: Object,
}


You might be interested in the way deadpool-postgres keeps track of the StatementCache of the objects. That's where Pool::detach comes into play.

/// Structure holding a reference to all [`StatementCache`]s and providing
/// access for clearing all caches and removing single statements from them.
#[derive(Default, Debug)]
pub struct StatementCaches {
caches: Mutex<Vec<Weak<StatementCache>>>,
}


I just opened an issue for tracking an idea that's been in my head for quite some time now:

@bikeshedder bikeshedder added invalid The issue is invalid and has been raised in error. A-core Area: Core / deadpool labels Jun 18, 2024
@bikeshedder
Copy link
Owner

@JosiahParry Did that solve your issue? If so, please close this issue.

@JosiahParry
Copy link
Author

I haven't had the ability to verify. I ended up writing my own manager-esque thing using a DashMap to use.

FWIW I was (and still slightly am) interested in using deadpool with a pingora reverse proxy to scale backend instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-core Area: Core / deadpool invalid The issue is invalid and has been raised in error.
Projects
None yet
Development

No branches or pull requests

2 participants