rslock
rslock copied to clipboard
Feature request: allow rslock functionality on an already established Redis connection
I started implementing similar functionality to this library yesterday and then discovered rslock. Since I'm already using Redis in my app for other things, it feels like I can't really use rslock since it has different assumptions about how it should be setup (i.e. through LockManager::new()). I think you could make rslock more flexible for others if you put most of the functionality in a trait that could be implemented for an existing Redis connection object. I might take a stab at this in the near future and submit a PR.
Nice to have !
I'd like to have this feature :)
this is open for anyone to make a pull request for. i would propose doing this through a LockManager::new_with_redis_client or similar.
- How:
put most of the functionality in a trait that could be implemented for an existing Redis connection object
would make things easier?
would make things easier?
May be add interface instead of implementation? Like this:
pub struct LockManager<C: AsyncCommands> {
lock_manager_inner: Arc<LockManagerInner<C>>,
retry_count: u32,
retry_delay: Duration,
}
#[derive(Debug, Clone)]
struct LockManagerInner<C: AsyncCommands> {
/// List of all Redis clients
pub servers: Vec<C>,
quorum: u32,
}
pub fn new<C: AsyncCommands>(clients: Vec<C>) -> LockManager<C> {
let quorum = (clients.len() as u32) / 2 + 1;
LockManager {
lock_manager_inner: Arc::new(LockManagerInner { servers: clients, quorum }),
retry_count: DEFAULT_RETRY_COUNT,
retry_delay: DEFAULT_RETRY_DELAY,
}
}
And in our code:
async fn main() {
// Now, create connection it our scope:
let manager = RedisConnectionManager::new("redis://127.0.0.1:6380/").expect("Fail while create manager");
let pool = bb8::Pool::builder().build(manager).await.expect("Fail while create pool");
let servers = pool.get().await?.to_owned();
// Or copy from constructor in current implementation
let servers: Vec<Client> = vec!["redis://127.0.0.1:6380/", "redis://127.0.0.1:6381/", "redis://127.0.0.1:6382/"]
.into_iter()
.map(|uri| Client::open(uri).unwrap())
.collect();
// Just pass client supports redis commands into lock manager
let red_lock = LockManager::new(servers);
}
This just dirty sketch with compilation error, but it reflects the thought.
@jlandahl, by the way, what do you think about this? Maybe you'll do something similar?
UPD: Oh my blinders... 2023 year... Im left to do PR...
@i8enn are you working on a PR by any chance? I'm interested in this too, and would love to help if possible.
Released in v0.6.0