Struct redsync::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)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
fn run() -> Result<(), Box<dyn Error>> {
    let dlm = Redsync::new(vec![
        RedisInstance::new("redis://127.0.0.1:6389")?,
        RedisInstance::new("redis://127.0.0.1:6399")?,
        RedisInstance::new("redis://127.0.0.1:6379")?,
    ]);

    let lock1 = dlm
        .lock("resource", Duration::from_secs(1))
        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
    println!("[t = 0] Acquired 1st lock for 1 second!");

    println!("[t = 0] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    let lock2 = dlm
        .lock("resource", Duration::from_secs(2))
        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
    println!("[t = 1] Acquired 2nd lock for 2 seconds!");

    println!("[t = 1] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    match dlm.unlock(&lock1) {
        Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
        Err(RedsyncError::UnlockFailed(err)) => {
            if err.includes(RedsyncError::InvalidLease) {
                println!("[t = 2] Failed to release 1st lock. Lock has expired!")
            }
        }
        Err(err) => println!("[t = 2] Unexpected error: {}", err),
    };

    dlm.extend(&lock2, Duration::from_secs(2))
        .map_err(|err| format!("Failed to extend lock on resource: {}", err))?;
    println!("[t = 2] Extended 2nd lock for 2 seconds!");

    println!("[t = 2] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    dlm.unlock(&lock2)
        .map_err(|err| format!("Failed to release lock on resource: {}", err))?;
    println!("[t = 3] Released 2nd lock after 1 second!");

    Ok(())
}
source

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

Examples found in repository?
examples/example.rs (line 21)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
fn run() -> Result<(), Box<dyn Error>> {
    let dlm = Redsync::new(vec![
        RedisInstance::new("redis://127.0.0.1:6389")?,
        RedisInstance::new("redis://127.0.0.1:6399")?,
        RedisInstance::new("redis://127.0.0.1:6379")?,
    ]);

    let lock1 = dlm
        .lock("resource", Duration::from_secs(1))
        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
    println!("[t = 0] Acquired 1st lock for 1 second!");

    println!("[t = 0] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    let lock2 = dlm
        .lock("resource", Duration::from_secs(2))
        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
    println!("[t = 1] Acquired 2nd lock for 2 seconds!");

    println!("[t = 1] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    match dlm.unlock(&lock1) {
        Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
        Err(RedsyncError::UnlockFailed(err)) => {
            if err.includes(RedsyncError::InvalidLease) {
                println!("[t = 2] Failed to release 1st lock. Lock has expired!")
            }
        }
        Err(err) => println!("[t = 2] Unexpected error: {}", err),
    };

    dlm.extend(&lock2, Duration::from_secs(2))
        .map_err(|err| format!("Failed to extend lock on resource: {}", err))?;
    println!("[t = 2] Extended 2nd lock for 2 seconds!");

    println!("[t = 2] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    dlm.unlock(&lock2)
        .map_err(|err| format!("Failed to release lock on resource: {}", err))?;
    println!("[t = 3] Released 2nd lock after 1 second!");

    Ok(())
}
source

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

Examples found in repository?
examples/example.rs (line 46)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
fn run() -> Result<(), Box<dyn Error>> {
    let dlm = Redsync::new(vec![
        RedisInstance::new("redis://127.0.0.1:6389")?,
        RedisInstance::new("redis://127.0.0.1:6399")?,
        RedisInstance::new("redis://127.0.0.1:6379")?,
    ]);

    let lock1 = dlm
        .lock("resource", Duration::from_secs(1))
        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
    println!("[t = 0] Acquired 1st lock for 1 second!");

    println!("[t = 0] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    let lock2 = dlm
        .lock("resource", Duration::from_secs(2))
        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
    println!("[t = 1] Acquired 2nd lock for 2 seconds!");

    println!("[t = 1] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    match dlm.unlock(&lock1) {
        Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
        Err(RedsyncError::UnlockFailed(err)) => {
            if err.includes(RedsyncError::InvalidLease) {
                println!("[t = 2] Failed to release 1st lock. Lock has expired!")
            }
        }
        Err(err) => println!("[t = 2] Unexpected error: {}", err),
    };

    dlm.extend(&lock2, Duration::from_secs(2))
        .map_err(|err| format!("Failed to extend lock on resource: {}", err))?;
    println!("[t = 2] Extended 2nd lock for 2 seconds!");

    println!("[t = 2] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    dlm.unlock(&lock2)
        .map_err(|err| format!("Failed to release lock on resource: {}", err))?;
    println!("[t = 3] Released 2nd lock after 1 second!");

    Ok(())
}
source

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

Examples found in repository?
examples/example.rs (line 36)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
fn run() -> Result<(), Box<dyn Error>> {
    let dlm = Redsync::new(vec![
        RedisInstance::new("redis://127.0.0.1:6389")?,
        RedisInstance::new("redis://127.0.0.1:6399")?,
        RedisInstance::new("redis://127.0.0.1:6379")?,
    ]);

    let lock1 = dlm
        .lock("resource", Duration::from_secs(1))
        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
    println!("[t = 0] Acquired 1st lock for 1 second!");

    println!("[t = 0] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    let lock2 = dlm
        .lock("resource", Duration::from_secs(2))
        .map_err(|err| format!("Failed to acquire lock on resource: {}", err))?;
    println!("[t = 1] Acquired 2nd lock for 2 seconds!");

    println!("[t = 1] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    match dlm.unlock(&lock1) {
        Ok(()) => println!("[t = 2] Released 1st lock after 2 seconds!"),
        Err(RedsyncError::UnlockFailed(err)) => {
            if err.includes(RedsyncError::InvalidLease) {
                println!("[t = 2] Failed to release 1st lock. Lock has expired!")
            }
        }
        Err(err) => println!("[t = 2] Unexpected error: {}", err),
    };

    dlm.extend(&lock2, Duration::from_secs(2))
        .map_err(|err| format!("Failed to extend lock on resource: {}", err))?;
    println!("[t = 2] Extended 2nd lock for 2 seconds!");

    println!("[t = 2] Sleeping for 1 second!");
    thread::sleep(Duration::from_secs(1));

    dlm.unlock(&lock2)
        .map_err(|err| format!("Failed to release lock on resource: {}", err))?;
    println!("[t = 3] Released 2nd lock after 1 second!");

    Ok(())
}

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

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

§

fn vzip(self) -> V