Redsync

Struct Redsync 

Source
pub struct Redsync<I: Instance> { /* private fields */ }
Expand description

Redsync is a distributed lock manager that implements the Redlock algorithm.

Implementations§

Source§

impl<I: Instance> Redsync<I>

Source

pub fn new(cluster: Vec<I>) -> Self

Examples found in repository?
examples/example.rs (lines 14-18)
13fn run() -> Result<(), Box<dyn Error>> {
14    let dlm = Redsync::new(vec![
15        RedisInstance::new("redis://127.0.0.1:6389")?,
16        RedisInstance::new("redis://127.0.0.1:6399")?,
17        RedisInstance::new("redis://127.0.0.1:6379")?,
18    ]);
19
20    let lock1 = dlm
21        .lock("resource", Duration::from_secs(1))
22        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
23    println!("[t = 0] Acquired 1st lock for 1 second!");
24
25    println!("[t = 0] Sleeping for 1 second!");
26    thread::sleep(Duration::from_secs(1));
27
28    let lock2 = dlm
29        .lock("resource", Duration::from_secs(2))
30        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
31    println!("[t = 1] Acquired 2nd lock for 2 seconds!");
32
33    println!("[t = 1] Sleeping for 1 second!");
34    thread::sleep(Duration::from_secs(1));
35
36    match dlm.unlock(&lock1) {
37        Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
38        Err(RedsyncError::UnlockFailed(err)) => {
39            if err.includes(RedsyncError::InvalidLease) {
40                println!("[t = 2] Failed to release 1st lock. Lock has expired!")
41            }
42        }
43        Err(err) => println!("[t = 2] Unexpected error: {}", err),
44    };
45
46    dlm.extend(&lock2, Duration::from_secs(2))
47        .map_err(|err| format!("Failed to extend lock on resource: {}", err))?;
48    println!("[t = 2] Extended 2nd lock for 2 seconds!");
49
50    println!("[t = 2] Sleeping for 1 second!");
51    thread::sleep(Duration::from_secs(1));
52
53    dlm.unlock(&lock2)
54        .map_err(|err| format!("Failed to release lock on resource: {}", err))?;
55    println!("[t = 3] Released 2nd lock after 1 second!");
56
57    Ok(())
58}
Source

pub fn lock(&self, resource: &str, ttl: Duration) -> Result<Lock, RedsyncError>

Examples found in repository?
examples/example.rs (line 21)
13fn run() -> Result<(), Box<dyn Error>> {
14    let dlm = Redsync::new(vec![
15        RedisInstance::new("redis://127.0.0.1:6389")?,
16        RedisInstance::new("redis://127.0.0.1:6399")?,
17        RedisInstance::new("redis://127.0.0.1:6379")?,
18    ]);
19
20    let lock1 = dlm
21        .lock("resource", Duration::from_secs(1))
22        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
23    println!("[t = 0] Acquired 1st lock for 1 second!");
24
25    println!("[t = 0] Sleeping for 1 second!");
26    thread::sleep(Duration::from_secs(1));
27
28    let lock2 = dlm
29        .lock("resource", Duration::from_secs(2))
30        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
31    println!("[t = 1] Acquired 2nd lock for 2 seconds!");
32
33    println!("[t = 1] Sleeping for 1 second!");
34    thread::sleep(Duration::from_secs(1));
35
36    match dlm.unlock(&lock1) {
37        Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
38        Err(RedsyncError::UnlockFailed(err)) => {
39            if err.includes(RedsyncError::InvalidLease) {
40                println!("[t = 2] Failed to release 1st lock. Lock has expired!")
41            }
42        }
43        Err(err) => println!("[t = 2] Unexpected error: {}", err),
44    };
45
46    dlm.extend(&lock2, Duration::from_secs(2))
47        .map_err(|err| format!("Failed to extend lock on resource: {}", err))?;
48    println!("[t = 2] Extended 2nd lock for 2 seconds!");
49
50    println!("[t = 2] Sleeping for 1 second!");
51    thread::sleep(Duration::from_secs(1));
52
53    dlm.unlock(&lock2)
54        .map_err(|err| format!("Failed to release lock on resource: {}", err))?;
55    println!("[t = 3] Released 2nd lock after 1 second!");
56
57    Ok(())
58}
Source

pub fn extend(&self, lock: &Lock, ttl: Duration) -> Result<Lock, RedsyncError>

Examples found in repository?
examples/example.rs (line 46)
13fn run() -> Result<(), Box<dyn Error>> {
14    let dlm = Redsync::new(vec![
15        RedisInstance::new("redis://127.0.0.1:6389")?,
16        RedisInstance::new("redis://127.0.0.1:6399")?,
17        RedisInstance::new("redis://127.0.0.1:6379")?,
18    ]);
19
20    let lock1 = dlm
21        .lock("resource", Duration::from_secs(1))
22        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
23    println!("[t = 0] Acquired 1st lock for 1 second!");
24
25    println!("[t = 0] Sleeping for 1 second!");
26    thread::sleep(Duration::from_secs(1));
27
28    let lock2 = dlm
29        .lock("resource", Duration::from_secs(2))
30        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
31    println!("[t = 1] Acquired 2nd lock for 2 seconds!");
32
33    println!("[t = 1] Sleeping for 1 second!");
34    thread::sleep(Duration::from_secs(1));
35
36    match dlm.unlock(&lock1) {
37        Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
38        Err(RedsyncError::UnlockFailed(err)) => {
39            if err.includes(RedsyncError::InvalidLease) {
40                println!("[t = 2] Failed to release 1st lock. Lock has expired!")
41            }
42        }
43        Err(err) => println!("[t = 2] Unexpected error: {}", err),
44    };
45
46    dlm.extend(&lock2, Duration::from_secs(2))
47        .map_err(|err| format!("Failed to extend lock on resource: {}", err))?;
48    println!("[t = 2] Extended 2nd lock for 2 seconds!");
49
50    println!("[t = 2] Sleeping for 1 second!");
51    thread::sleep(Duration::from_secs(1));
52
53    dlm.unlock(&lock2)
54        .map_err(|err| format!("Failed to release lock on resource: {}", err))?;
55    println!("[t = 3] Released 2nd lock after 1 second!");
56
57    Ok(())
58}
Source

pub fn unlock(&self, lock: &Lock) -> Result<(), RedsyncError>

Examples found in repository?
examples/example.rs (line 36)
13fn run() -> Result<(), Box<dyn Error>> {
14    let dlm = Redsync::new(vec![
15        RedisInstance::new("redis://127.0.0.1:6389")?,
16        RedisInstance::new("redis://127.0.0.1:6399")?,
17        RedisInstance::new("redis://127.0.0.1:6379")?,
18    ]);
19
20    let lock1 = dlm
21        .lock("resource", Duration::from_secs(1))
22        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
23    println!("[t = 0] Acquired 1st lock for 1 second!");
24
25    println!("[t = 0] Sleeping for 1 second!");
26    thread::sleep(Duration::from_secs(1));
27
28    let lock2 = dlm
29        .lock("resource", Duration::from_secs(2))
30        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
31    println!("[t = 1] Acquired 2nd lock for 2 seconds!");
32
33    println!("[t = 1] Sleeping for 1 second!");
34    thread::sleep(Duration::from_secs(1));
35
36    match dlm.unlock(&lock1) {
37        Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
38        Err(RedsyncError::UnlockFailed(err)) => {
39            if err.includes(RedsyncError::InvalidLease) {
40                println!("[t = 2] Failed to release 1st lock. Lock has expired!")
41            }
42        }
43        Err(err) => println!("[t = 2] Unexpected error: {}", err),
44    };
45
46    dlm.extend(&lock2, Duration::from_secs(2))
47        .map_err(|err| format!("Failed to extend lock on resource: {}", err))?;
48    println!("[t = 2] Extended 2nd lock for 2 seconds!");
49
50    println!("[t = 2] Sleeping for 1 second!");
51    thread::sleep(Duration::from_secs(1));
52
53    dlm.unlock(&lock2)
54        .map_err(|err| format!("Failed to release lock on resource: {}", err))?;
55    println!("[t = 3] Released 2nd lock after 1 second!");
56
57    Ok(())
58}

Auto Trait Implementations§

§

impl<I> Freeze for Redsync<I>

§

impl<I> RefUnwindSafe for Redsync<I>
where I: RefUnwindSafe,

§

impl<I> Send for Redsync<I>
where I: Send,

§

impl<I> Sync for Redsync<I>
where I: Sync,

§

impl<I> Unpin for Redsync<I>
where I: Unpin,

§

impl<I> UnwindSafe for Redsync<I>
where I: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> ErasedDestructor for T
where T: 'static,