pub struct RedisInstance { /* private fields */ }
Expand description
RedisInstance
is the implementation of the Instance trait for a Redis server.
Implementations§
Source§impl RedisInstance
impl RedisInstance
Sourcepub fn new<T: IntoConnectionInfo>(params: T) -> Result<Self, RedsyncError>
pub fn new<T: IntoConnectionInfo>(params: T) -> Result<Self, RedsyncError>
Examples found in repository?
examples/example.rs (line 15)
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}
Trait Implementations§
Auto Trait Implementations§
impl Freeze for RedisInstance
impl RefUnwindSafe for RedisInstance
impl Send for RedisInstance
impl Sync for RedisInstance
impl Unpin for RedisInstance
impl UnwindSafe for RedisInstance
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more