forked from datenlord/etcd-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the lock operations (datenlord#17)
- Loading branch information
Jicheng Shi
committed
Mar 14, 2021
1 parent
16cc8c6
commit c9f2545
Showing
5 changed files
with
265 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/// The mod of lock release operations | ||
mod release; | ||
/// The mod of lock require operations | ||
mod require; | ||
|
||
use crate::protos::lock_grpc::LockClient; | ||
use crate::Result as Res; | ||
pub use release::{EtcdUnlockRequest, EtcdUnlockResponse}; | ||
pub use require::{EtcdLockRequest, EtcdLockResponse}; | ||
|
||
/// Lock client. | ||
#[derive(Clone)] | ||
pub struct Lock { | ||
/// Etcd Lock client. | ||
client: LockClient, | ||
} | ||
|
||
impl Lock { | ||
/// Creates a new `LockClient`. | ||
/// | ||
/// This method should only be called within etcd client. | ||
pub(crate) const fn new(client: LockClient) -> Self { | ||
Self { client } | ||
} | ||
|
||
/// Performs a lock operation. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Will return `Err` if RPC call is failed. | ||
#[inline] | ||
pub async fn lock(&mut self, req: EtcdLockRequest) -> Res<EtcdLockResponse> { | ||
let resp = self.client.lock_async(&req.into())?.await?; | ||
Ok(From::from(resp)) | ||
} | ||
|
||
/// Performs a unlock operation. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Will return `Err` if RPC call is failed. | ||
#[inline] | ||
pub async fn unlock(&mut self, req: EtcdUnlockRequest) -> Res<EtcdUnlockResponse> { | ||
let resp = self.client.unlock_async(&req.into())?.await?; | ||
Ok(From::from(resp)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::protos::lock::{UnlockRequest, UnlockResponse}; | ||
use crate::ResponseHeader; | ||
|
||
/// Request for requiring a lock | ||
pub struct EtcdUnlockRequest { | ||
/// Etcd lock request | ||
proto: UnlockRequest, | ||
} | ||
|
||
impl EtcdUnlockRequest { | ||
/// Creates a new `EtcdUnlockRequest` for requiring a lock | ||
#[inline] | ||
pub fn new<T>(key: T) -> Self | ||
where | ||
T: Into<Vec<u8>>, | ||
{ | ||
let lock_request = UnlockRequest { | ||
key: key.into(), | ||
..UnlockRequest::default() | ||
}; | ||
|
||
Self { | ||
proto: lock_request, | ||
} | ||
} | ||
|
||
/// Get the name from `UnlockRequest` | ||
#[inline] | ||
pub fn get_key(&self) -> Vec<u8> { | ||
self.proto.get_key().to_vec() | ||
} | ||
} | ||
|
||
impl Into<UnlockRequest> for EtcdUnlockRequest { | ||
#[inline] | ||
fn into(self) -> UnlockRequest { | ||
self.proto | ||
} | ||
} | ||
|
||
/// Response for requring a lock. | ||
#[derive(Debug)] | ||
pub struct EtcdUnlockResponse { | ||
/// Etcd lock response | ||
proto: UnlockResponse, | ||
} | ||
|
||
impl EtcdUnlockResponse { | ||
/// Takes the header out of response, leaving a `None` in its place. | ||
#[inline] | ||
pub fn take_header(&mut self) -> Option<ResponseHeader> { | ||
match self.proto.header.take() { | ||
Some(header) => Some(From::from(header)), | ||
None => None, | ||
} | ||
} | ||
} | ||
|
||
impl From<UnlockResponse> for EtcdUnlockResponse { | ||
#[inline] | ||
fn from(resp: UnlockResponse) -> Self { | ||
Self { proto: resp } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::protos::lock::{LockRequest, LockResponse}; | ||
use crate::ResponseHeader; | ||
use utilities::Cast; | ||
|
||
/// Request for requiring a lock | ||
pub struct EtcdLockRequest { | ||
/// Etcd lock request | ||
proto: LockRequest, | ||
} | ||
|
||
impl EtcdLockRequest { | ||
/// Creates a new `EtcdLockRequest` for requiring a lock | ||
#[inline] | ||
pub fn new<T>(name: T, lease: u64) -> Self | ||
where | ||
T: Into<Vec<u8>>, | ||
{ | ||
let lock_request = LockRequest { | ||
name: name.into(), | ||
lease: lease.cast(), | ||
..LockRequest::default() | ||
}; | ||
|
||
Self { | ||
proto: lock_request, | ||
} | ||
} | ||
|
||
/// Get the name from `LockRequest` | ||
#[inline] | ||
pub fn get_name(&self) -> Vec<u8> { | ||
self.proto.get_name().to_vec() | ||
} | ||
|
||
/// Get the name from `LockRequest` | ||
#[inline] | ||
pub fn get_lease(&self) -> u64 { | ||
self.proto.get_lease().cast() | ||
} | ||
} | ||
|
||
impl Into<LockRequest> for EtcdLockRequest { | ||
#[inline] | ||
fn into(self) -> LockRequest { | ||
self.proto | ||
} | ||
} | ||
|
||
/// Response for requring a lock. | ||
#[derive(Debug)] | ||
pub struct EtcdLockResponse { | ||
/// Etcd lock response | ||
proto: LockResponse, | ||
} | ||
|
||
impl EtcdLockResponse { | ||
/// Takes the header out of response, leaving a `None` in its place. | ||
#[inline] | ||
pub fn take_header(&mut self) -> Option<ResponseHeader> { | ||
match self.proto.header.take() { | ||
Some(header) => Some(From::from(header)), | ||
None => None, | ||
} | ||
} | ||
|
||
/// Take the key out of response, leaving a empty Vec in its place. | ||
#[inline] | ||
pub fn take_key(&mut self) -> Vec<u8> { | ||
self.proto.take_key() | ||
} | ||
} | ||
|
||
impl From<LockResponse> for EtcdLockResponse { | ||
#[inline] | ||
fn from(resp: LockResponse) -> Self { | ||
Self { proto: resp } | ||
} | ||
} |