bikeshedder / deadpool

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

Keep track of all objects #336

Open bikeshedder opened 5 months ago

bikeshedder commented 5 months ago

Right now objects which are handed out from the pool keep a Weak reference to the pool but the pool has no way of referencing the Object. In order to implement the StatementCaches in deadpool-postgres I had to put the StatementCache of each object in an Arc and add the logic of keeping track of objects to the Manager itself.

It would be nice if the Pool kept a Weak reference to all objects. This would also allow for deeper introspection. e.g. accessing the metrics of objects which are in use by other workers.

The downside would be that all objects would now include an indirection via an Arc to the inner object even if it is never used. It would however open the library up for things like sharing objects:

It might make other features more complicated though as some objects need &mut self and wrapping it inside an Arc<T> would now require a Arc<Mutex<T>> which is a hefty price for code that doesn't need this:

Ideally there would be three implementations giving the user the choice:

Alternatively the object could be split into two parts. The first part is the object itself which just needs to be Send and only one worker is allowed to access it. The other part consists of metrics and other pool specific things which can be accessed from multiple workers and is required to be Send + Sync.

This feature has been in my head for quite some time but I've never come around writing it down.