Skip to content

Commit

Permalink
feat: add a new xline-client crate
Browse files Browse the repository at this point in the history
This commit add the xline client with placeholders, currently no client function is implemented.
  • Loading branch information
bsbds committed Jun 7, 2023
1 parent 2f0b026 commit a99a5eb
Show file tree
Hide file tree
Showing 15 changed files with 763 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]

members = ["xline", "curp", "benchmark", "utils", "engine", "xlineapi"]
members = ["xline", "curp", "benchmark", "utils", "engine", "xlineapi", "xline-client"]
31 changes: 31 additions & 0 deletions xline-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "xline-client"
version = "0.1.0"
edition = "2021"
authors = ["DatenLord <dev@datenlord.io>"]
description = "Client for Xline"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/datenlord/Xline/tree/master/xline-client"
categories = ["Client"]
keywords = ["Client", "Xline", "RPC"]

[dependencies]
curp = { path = "../curp" }
xline = { path = "../xline" }
utils = { path = "../utils", features = ["parking_lot"] }
xlineapi = { path = "../xlineapi" }
tonic = "0.7.2"
itertools = "0.10.3"
uuid = { version = "1.1.2", features = ["v4"] }
thiserror = "1.0.37"
tower = { version = "0.4", features = ["discover"] }
tokio = { version = "1.0", features = ["sync"] }
futures = "0.3.25"
clippy-utilities = "0.1.0"
async-stream = "0.3.5"
pbkdf2 = { version = "0.11.0", features = ["std"] }
http = "0.2.9"

[dev-dependencies]
rand = "0.8.5"
3 changes: 3 additions & 0 deletions xline-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Xline Client

This crate provides a client for Xline server.
44 changes: 44 additions & 0 deletions xline-client/src/clients/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// TODO: Remove these when the placeholder is implemented.
#![allow(dead_code)]

use std::sync::Arc;

use curp::client::Client as CurpClient;
use tonic::transport::Channel;
use xline::server::Command;

use crate::AuthService;

/// Client for Auth operations.
#[derive(Clone, Debug)]
pub struct AuthClient {
/// Name of the AuthClient
name: String,
/// The inner CURP client
curp_client: Arc<CurpClient<Command>>,
/// The standalone client
standalone_client: xlineapi::AuthClient<AuthService<Channel>>,
/// Auth token
token: Option<String>,
}

impl AuthClient {
/// New `AuthClient`
#[inline]
pub fn new(
name: String,
curp_client: Arc<CurpClient<Command>>,
channel: Channel,
token: Option<String>,
) -> Self {
Self {
name,
curp_client,
standalone_client: xlineapi::AuthClient::new(AuthService::new(
channel,
token.as_ref().and_then(|t| t.parse().ok().map(Arc::new)),
)),
token,
}
}
}
17 changes: 17 additions & 0 deletions xline-client/src/clients/cluster.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TODO: Remove these when the placeholder is implemented.
#![allow(missing_copy_implementations)]
#![allow(clippy::new_without_default)]

/// The cluster client
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ClusterClient;

impl ClusterClient {
/// Create a new cluster client
#[inline]
#[must_use]
pub fn new() -> Self {
Self
}
}
17 changes: 17 additions & 0 deletions xline-client/src/clients/election.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TODO: Remove these when the placeholder is implemented.
#![allow(missing_copy_implementations)]
#![allow(clippy::new_without_default)]

/// The election client
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct ElectionClient;

impl ElectionClient {
/// Create a new election client
#[inline]
#[must_use]
pub fn new() -> Self {
Self
}
}
30 changes: 30 additions & 0 deletions xline-client/src/clients/kv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// TODO: Remove these when the placeholder is implemented.
#![allow(dead_code)]

use std::sync::Arc;

use curp::client::Client as CurpClient;
use xline::server::Command;

/// Client for KV operations.
#[derive(Clone, Debug)]
pub struct KvClient {
/// Name of the KvClient
name: String,
/// The inner CURP client
curp_client: Arc<CurpClient<Command>>,
/// Auth token
token: Option<String>,
}

impl KvClient {
/// New `KvClient`
#[inline]
pub fn new(name: String, curp_client: Arc<CurpClient<Command>>, token: Option<String>) -> Self {
Self {
name,
curp_client,
token,
}
}
}
44 changes: 44 additions & 0 deletions xline-client/src/clients/lease.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// TODO: Remove these when the placeholder is implemented.
#![allow(dead_code)]

use std::sync::Arc;

use curp::client::Client as CurpClient;
use tonic::transport::Channel;
use xline::server::Command;

use crate::AuthService;

/// Client for Lease operations.
#[derive(Clone, Debug)]
pub struct LeaseClient {
/// Name of the LeaseClient
name: String,
/// The CURP client
curp_client: Arc<CurpClient<Command>>,
/// The standalone client
standalone_client: xlineapi::LeaseClient<AuthService<Channel>>,
/// Auth token
token: Option<String>,
}

impl LeaseClient {
/// New `LeaseClient`
#[inline]
pub fn new(
name: String,
curp_client: Arc<CurpClient<Command>>,
channel: Channel,
token: Option<String>,
) -> Self {
Self {
name,
curp_client,
standalone_client: xlineapi::LeaseClient::new(AuthService::new(
channel,
token.as_ref().and_then(|t| t.parse().ok().map(Arc::new)),
)),
token,
}
}
}
41 changes: 41 additions & 0 deletions xline-client/src/clients/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// TODO: Remove these when the placeholder is implemented.
#![allow(dead_code)]

use std::sync::Arc;

use curp::client::Client as CurpClient;
use tonic::transport::Channel;
use xline::server::Command;

use crate::clients::watch::WatchClient;

/// Client for Lock operations.
#[derive(Clone, Debug)]
pub struct LockClient {
/// Name of the LockClient
name: String,
/// The inner CURP client
curp_client: Arc<CurpClient<Command>>,
/// The watch client
watch_client: WatchClient,
/// Auth token
token: Option<String>,
}

impl LockClient {
/// New `LockClient`
#[inline]
pub fn new(
name: String,
curp_client: Arc<CurpClient<Command>>,
channel: Channel,
token: Option<String>,
) -> Self {
Self {
name,
curp_client,
watch_client: WatchClient::new(channel, token.clone()),
token,
}
}
}
29 changes: 29 additions & 0 deletions xline-client/src/clients/maintenance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// TODO: Remove these when the placeholder is implemented.
#![allow(dead_code)]

use std::{fmt::Debug, sync::Arc};

use tonic::transport::Channel;

use crate::AuthService;

/// The maintenance client
#[derive(Clone, Debug)]
pub struct MaintenanceClient {
/// the inner RPC client
inner: xlineapi::MaintenanceClient<AuthService<Channel>>,
}

impl MaintenanceClient {
/// Create a new maintenance client
#[inline]
#[must_use]
pub fn new(channel: Channel, token: Option<String>) -> Self {
Self {
inner: xlineapi::MaintenanceClient::new(AuthService::new(
channel,
token.and_then(|t| t.parse().ok().map(Arc::new)),
)),
}
}
}
16 changes: 16 additions & 0 deletions xline-client/src/clients/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// Auth client.
pub mod auth;
/// Cluster client
pub mod cluster;
/// Election client.
pub mod election;
/// Kv client.
pub mod kv;
/// Lease client.
pub mod lease;
/// Lock client.
pub mod lock;
/// Maintenance client.
pub mod maintenance;
/// Watch client.
pub mod watch;
29 changes: 29 additions & 0 deletions xline-client/src/clients/watch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// TODO: Remove these when the placeholder is implemented.
#![allow(dead_code)]

use std::{fmt::Debug, sync::Arc};

use tonic::transport::Channel;

use crate::AuthService;

/// The maintenance client
#[derive(Clone, Debug)]
pub struct WatchClient {
/// The inner RPC client
inner: xlineapi::WatchClient<AuthService<Channel>>,
}

impl WatchClient {
/// Create a new maintenance client
#[inline]
#[must_use]
pub fn new(channel: Channel, token: Option<String>) -> Self {
Self {
inner: xlineapi::WatchClient::new(AuthService::new(
channel,
token.and_then(|t| t.parse().ok().map(Arc::new)),
)),
}
}
}
42 changes: 42 additions & 0 deletions xline-client/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use thiserror::Error;

/// Xline client result
pub type Result<T> = std::result::Result<T, ClientError>;

/// Client Error
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ClientError {
/// Propose error
#[error("propose error {0}")]
ProposeError(#[from] curp::error::ProposeError),
/// IO error
#[error("IO error {0}")]
IoError(#[from] std::io::Error),
/// Rpc Error
#[error("rpc error: {0}")]
RpcError(String),
/// Arguments invalid Error
#[error("Invalid arguments: {0}")]
InvalidArgs(String),
/// error in watch client
#[error("Watch client error: {0}")]
WatchError(String),
/// error in lease client
#[error("Lease client error: {0}")]
LeaseError(String),
}

impl From<tonic::transport::Error> for ClientError {
#[inline]
fn from(e: tonic::transport::Error) -> Self {
Self::RpcError(e.to_string())
}
}

impl From<tonic::Status> for ClientError {
#[inline]
fn from(e: tonic::Status) -> Self {
Self::RpcError(e.to_string())
}
}
Loading

0 comments on commit a99a5eb

Please sign in to comment.