Xline
Xline copied to clipboard
[Refactor]: Remove request wrappers in xline-client
We implement some request wrappers in the xline-client crate, like
/// Xline/crates/xline-client/src/types/lock.rs
/// Request for `Lock`
#[derive(Debug, PartialEq)]
pub struct LockRequest {
/// The inner request
pub(crate) inner: xlineapi::LockRequest,
/// The ttl of the lease that attached to the lock
pub(crate) ttl: i64,
}
impl LockRequest {
/// Creates a new `LockRequest`
#[inline]
#[must_use]
pub fn new(name: impl Into<Vec<u8>>) -> Self {
...
}
/// Set lease.
#[inline]
#[must_use]
pub const fn with_lease(mut self, lease: i64) -> Self {
self.inner.lease = lease;
self
}
/// Set session TTL.
/// Will be ignored when lease id is set
#[inline]
#[must_use]
pub const fn with_ttl(mut self, ttl: i64) -> Self {
self.ttl = ttl;
self
}
}
Therefore, when we want to perform a lock operation by xline-client, we should write some code like this:
#[tokio::main]
async fn main() -> Result<()> {
...
let lock_request = LockRequest::new("lock-test").with_ttl(50);
// acquire a lock session
let session = lock_client
.lock(lock_request)
.await?;
....
Ok(())
}
This way is not very user-friendly. I prefer to use lock_client.lock("lock-test", 50)
rather than lock_client.lock(LockRequest::new("lock-test").with_ttl(50))
.