rslock icon indicating copy to clipboard operation
rslock copied to clipboard

Feature request: allow rslock functionality on an already established Redis connection

Open jlandahl opened this issue 2 years ago • 6 comments

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.

jlandahl avatar Sep 10 '23 18:09 jlandahl

Nice to have !

florianepitech avatar Mar 05 '24 16:03 florianepitech

I'd like to have this feature :)

itaispiegel avatar Jun 20 '24 11:06 itaispiegel

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.

hexcowboy avatar Jun 20 '24 15:06 hexcowboy

  1. How:

put most of the functionality in a trait that could be implemented for an existing Redis connection object

would make things easier?

legas7 avatar Aug 05 '24 13:08 legas7

would make things easier?

May be add interface instead of implementation? Like this:

In lib:

 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 avatar Oct 03 '24 15:10 i8enn

@i8enn are you working on a PR by any chance? I'm interested in this too, and would love to help if possible.

FireMasterK avatar Oct 07 '24 20:10 FireMasterK

Released in v0.6.0

hexcowboy avatar Nov 18 '24 02:11 hexcowboy