Struct redsync::RedisInstance
source · 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)
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(())
}