Skip to content

Commit

Permalink
chore: resolve some comments
Browse files Browse the repository at this point in the history
Signed-off-by: Phoeniix Zhao <Phoenix500526@163.com>
  • Loading branch information
Phoenix500526 committed May 17, 2024
1 parent bddfaab commit 1c973bf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
7 changes: 2 additions & 5 deletions crates/xline-client/examples/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
29 changes: 14 additions & 15 deletions crates/xline-client/src/clients/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ 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 {
const MILLISECOND_PER_SECOND: u64 = 1000;
let (mut keeper, mut stream) = lease_client
.keep_alive(LeaseKeepAliveRequest::new(lease_id))
.await?;
Expand All @@ -96,7 +97,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() * MILLISECOND_PER_SECOND * 3 / 5,
))
.await;
}
}
}));
Expand Down Expand Up @@ -128,7 +132,7 @@ pub struct Xutex {
header: Option<ResponseHeader>,
}

/// 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
Expand All @@ -145,6 +149,13 @@ impl XutexGuard {
key,
})
}

/// Get the key of the Xutex
#[inline]
#[must_use]
pub fn key(&self) -> &str {
self.key.as_str()
}
}

#[async_trait]
Expand All @@ -171,13 +182,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<TxnResponse> {
self.key = format!("{prefix}{lease_id:x}");
Expand Down Expand Up @@ -326,16 +330,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.
Expand Down
2 changes: 0 additions & 2 deletions crates/xline-client/tests/it/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
6 changes: 2 additions & 4 deletions crates/xlinectl/src/command/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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, ");
Expand Down

0 comments on commit 1c973bf

Please sign in to comment.