Skip to content

Commit

Permalink
chore: use derive_builder to reduce boilerplate code
Browse files Browse the repository at this point in the history
  • Loading branch information
paomian committed May 21, 2024
1 parent fb9d1a1 commit 4a0bc67
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tokio = { version = "1", features = ["rt", "time"] }
tokio-stream = { version = "0.1", features = ["net"] }
tonic = { version = "0.11", features = ["tls", "tls-roots", "gzip", "zstd"] }
tower = "0.4"
derive_builder = "0.20.0"

[build-dependencies]
tonic-build = "0.9"
Expand Down
65 changes: 19 additions & 46 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tonic::transport::Channel;

use crate::load_balance::{LoadBalance, Loadbalancer};
use crate::{error, Result};
use derive_builder::Builder;

const MAX_MESSAGE_SIZE: usize = 512 * 1024 * 1024;

Expand Down Expand Up @@ -76,7 +77,8 @@ impl ClientBuilder {
.load_balance(self.load_balance)
.compression(self.compression)
.peers(self.peers)
.build();
.build()
.unwrap();
Client {
inner: Arc::new(inner),
}
Expand All @@ -87,7 +89,7 @@ impl ClientBuilder {
pub enum Compression {
Gzip,
Zstd,
Plain,
None,
}

impl Default for Compression {
Expand All @@ -96,14 +98,22 @@ impl Default for Compression {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Builder)]
struct Inner {
channel_manager: ChannelManager,
#[builder(setter(custom))]
peers: Arc<RwLock<Vec<String>>>,
load_balance: Loadbalancer,
compression: Compression,
}

impl InnerBuilder {
pub fn peers(&mut self, peers: Vec<String>) -> &mut Self {
self.peers = Some(Arc::new(RwLock::new(peers)));
self
}
}

impl Inner {
fn set_peers(&self, peers: Vec<String>) {
let mut guard = self.peers.write();
Expand All @@ -116,45 +126,6 @@ impl Inner {
}
}

#[derive(Default)]
pub struct InnerBuilder {
channel_manager: ChannelManager,
load_balance: Loadbalancer,
compression: Compression,
peers: Arc<RwLock<Vec<String>>>,
}

impl InnerBuilder {
pub(self) fn channel_manager(mut self, channel_manager: ChannelManager) -> Self {
self.channel_manager = channel_manager;
self
}

pub(self) fn load_balance(mut self, load_balance: Loadbalancer) -> Self {
self.load_balance = load_balance;
self
}

pub(self) fn compression(mut self, compression: Compression) -> Self {
self.compression = compression;
self
}

pub(self) fn peers(mut self, peers: Vec<String>) -> Self {
self.peers = Arc::new(RwLock::new(peers));
self
}

pub(self) fn build(self) -> Inner {
Inner {
channel_manager: self.channel_manager,
load_balance: self.load_balance,
compression: self.compression,
peers: self.peers,
}
}
}

impl Client {
#[deprecated(since = "0.1.0", note = "use `ClientBuilder` instead of this method")]
pub fn new() -> Self {
Expand All @@ -165,7 +136,8 @@ impl Client {
pub fn with_manager(channel_manager: ChannelManager) -> Self {
let inner = InnerBuilder::default()
.channel_manager(channel_manager)
.build();
.build()
.unwrap();
Self {
inner: Arc::new(inner),
}
Expand All @@ -189,14 +161,15 @@ impl Client {
let inner = InnerBuilder::default()
.channel_manager(channel_manager)
.peers(normailze_urls(urls))
.build();
.build()
.unwrap();

Self {
inner: Arc::new(inner),
}
}

#[deprecated(since = "0.1.0", note = "should be removed in the future")]
#[deprecated(since = "0.1.0", note = "use `ClientBuilder` instead of this method")]
pub fn start<U, A>(&self, urls: A)
where
U: AsRef<str>,
Expand Down Expand Up @@ -233,7 +206,7 @@ impl Client {
Compression::Zstd => {
client = client.send_compressed(CompressionEncoding::Zstd);
}
Compression::Plain => {}
Compression::None => {}
}
Ok(DatabaseClient { inner: client })
}
Expand Down

0 comments on commit 4a0bc67

Please sign in to comment.