diff --git a/crates/xline-client/examples/lock.rs b/crates/xline-client/examples/lock.rs index 874138cc0..ce47ab6e4 100644 --- a/crates/xline-client/examples/lock.rs +++ b/crates/xline-client/examples/lock.rs @@ -17,16 +17,13 @@ async fn main() -> Result<()> { let session = Session::new(client).build().await?; let mut xutex = Xutex::new(session, "lock-test"); - + // when the `xutex_guard` drop, the lock will be unlocked. let xutex_guard = xutex.lock().await?; println!( "lock key: {:?}", - String::from_utf8_lossy(xutex.key().as_bytes()) + String::from_utf8_lossy(xutex_guard.key().as_bytes()) ); - // drop `xutex_guard` or xutex.unlock().await? will release the lock - std::mem::drop(xutex_guard); - Ok(()) } diff --git a/crates/xline-client/src/clients/lock.rs b/crates/xline-client/src/clients/lock.rs index 82a1178e3..6871372f9 100644 --- a/crates/xline-client/src/clients/lock.rs +++ b/crates/xline-client/src/clients/lock.rs @@ -85,6 +85,8 @@ impl Session { let mut lease_client = self.client.lease_client.clone(); let lease_id = self.lease_id; self.keep_alive = Some(tokio::spawn(async move { + /// The renew interval factor of which value equals 60% of one second. + const RENEW_INTERVAL_FACTOR: u64 = 600; let (mut keeper, mut stream) = lease_client .keep_alive(LeaseKeepAliveRequest::new(lease_id)) .await?; @@ -96,7 +98,10 @@ impl Session { "lease keepalive response has negative ttl", ))); } - sleep(Duration::from_secs(resp.ttl.unsigned_abs() / 3)).await; + sleep(Duration::from_millis( + resp.ttl.unsigned_abs().overflow_mul(RENEW_INTERVAL_FACTOR), + )) + .await; } } })); @@ -128,7 +133,7 @@ pub struct Xutex { header: Option, } -/// An RAII implementation of `Xutex` +/// An RAII implementation of a “scoped lock” of an `Xutex` #[derive(Default, Debug)] pub struct XutexGuard { /// The lock client that used to unlock `key` when `XutexGuard` is dropped @@ -145,6 +150,13 @@ impl XutexGuard { key, }) } + + /// Get the key of the Xutex + #[inline] + #[must_use] + pub fn key(&self) -> &str { + self.key.as_str() + } } #[async_trait] @@ -171,13 +183,6 @@ impl Xutex { } } - /// Get the key of the Xutex - #[inline] - #[must_use] - pub fn key(&self) -> &str { - self.key.as_str() - } - /// try to acquire lock async fn try_acquire(&mut self, prefix: String, lease_id: i64) -> Result { self.key = format!("{prefix}{lease_id:x}"); @@ -326,16 +331,11 @@ impl Xutex { )) } Err(e) => { - self.unlock().await?; + self.session.client.delete_key(self.key.as_bytes()).await?; Err(e) } } } - - /// unlock the Xutex - async fn unlock(&self) -> Result<()> { - self.session.client.delete_key(self.key.as_bytes()).await - } } /// Client for Lock operations. diff --git a/crates/xline-client/tests/it/lock.rs b/crates/xline-client/tests/it/lock.rs index 790d854b7..7a40188cd 100644 --- a/crates/xline-client/tests/it/lock.rs +++ b/crates/xline-client/tests/it/lock.rs @@ -76,7 +76,5 @@ async fn lock_should_unlock_after_cancelled() -> Result<()> { .await .expect("timeout when trying to lock")?; - // assert!(session.response().key.starts_with(b"lock-test/")); - Ok(()) } diff --git a/crates/xlinectl/src/command/lock.rs b/crates/xlinectl/src/command/lock.rs index c9b2edceb..79a443415 100644 --- a/crates/xlinectl/src/command/lock.rs +++ b/crates/xlinectl/src/command/lock.rs @@ -6,8 +6,6 @@ use xline_client::{ Client, }; -// use crate::utils::printer::Printer; - /// Definition of `lock` command pub(crate) fn command() -> Command { Command::new("lock") @@ -21,8 +19,8 @@ pub(crate) async fn execute(client: &mut Client, matches: &ArgMatches) -> Result let session = Session::new(client.lock_client()).build().await?; let mut xutex = Xutex::new(session, prefix); - let _xutex_guard = xutex.lock().await?; - println!("{}", xutex.key()); + let xutex_guard = xutex.lock().await?; + println!("{}", xutex_guard.key()); signal::ctrl_c().await.expect("failed to listen for event"); // let res = lock_resp.unlock().await; println!("releasing the lock, ");