sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.49k stars 80 forks source link

"Named" connections #123

Closed uschi2000 closed 2 years ago

uschi2000 commented 2 years ago

Imagine a scenario where you have clients for a (small) number of different S3 buckets (alternatively, Azure blob containers, etc). Each bucket requires different credentials. I would write configuration like:

bucket_a:
  path: foo/bar/a
  token: abc123
bucket_b:
  path: boom/baz/b
  token xyz456

Then I'd love to say something like:

let config = read_config("config.yml")
let manager = r2d2_foodb::FooConnectionManager::new( /* a closure that takes a bucket name and returns the connection config */);
let pool = r2d2::Pool::builder()
    .max_size(15)
    .build(manager)
    .unwrap();

pool.get("bucket_a").unwrap().fetch("some_blob_from_bucket_a");
pool.get("bucket_a").unwrap().fetch("some_other_blob_from_bucket_a");
pool.get("bucket_b").unwrap().fetch("some_blob_from_bucket_b");

In other words a pool of "named" connections. Is this something you've been thinking about, @sfackler ? If not, do you have a suggestions for a cheap workaround/hack on top of r2d2? A map/cache of r2d2 pools?

sfackler commented 2 years ago

My instinct would be to have something like a map of r2d2 pools keyed off of bucket_a/bucket_b, etc. You'll probably want to have them share a thread pool, but once you do that it should behave pretty reasonably I think: https://docs.rs/r2d2/0.8.9/r2d2/struct.Builder.html#method.thread_pool

uschi2000 commented 2 years ago

Cool, yeah that's what I meant by A map/cache of r2d2 pools above. We'll play around with that. I assume you have no interest in supporting a notion of "named pools" in r2d2 APIs, right?

sfackler commented 2 years ago

Yeah, I think for now it'd make sense to keep it separate.