bikeshedder / deadpool

Dead simple pool implementation for rust with async-await
Apache License 2.0
1.08k stars 137 forks source link

Implement Manager on a different type than `Object` #335

Closed JosiahParry closed 5 months ago

JosiahParry commented 5 months ago

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 commented 5 months ago

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>):

https://github.com/bikeshedder/deadpool/blob/7b933c585d39f2ef57304353a35e139174b28f8e/src/managed/mod.rs#L249

https://github.com/bikeshedder/deadpool/blob/7b933c585d39f2ef57304353a35e139174b28f8e/src/managed/mod.rs#L311-L313

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

https://github.com/bikeshedder/deadpool/blob/7b933c585d39f2ef57304353a35e139174b28f8e/src/managed/mod.rs#L220-L225

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:

https://github.com/bikeshedder/deadpool/blob/7b933c585d39f2ef57304353a35e139174b28f8e/redis/src/lib.rs#L49-L56


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.

https://github.com/bikeshedder/deadpool/blob/7b933c585d39f2ef57304353a35e139174b28f8e/postgres/src/lib.rs#L233-L238


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

bikeshedder commented 5 months ago

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

JosiahParry commented 5 months ago

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.