xline-kv / Xline

A geo-distributed KV store for metadata management
https://xline.cloud
Apache License 2.0
562 stars 70 forks source link

[Refactor]: Remove request wrappers in xline-client #819

Open Phoenix500526 opened 1 month ago

Phoenix500526 commented 1 month ago

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)).

github-actions[bot] commented 1 month ago

👋 Thanks for opening this issue!

Reply with the following command on its own line to get help or engage:

realtaobo commented 3 weeks ago

/assignme