Skip to content

Commit

Permalink
Merge pull request #173 from 56quarters/defaults
Browse files Browse the repository at this point in the history
Add defaults for MemcachedClient generic parameters
  • Loading branch information
56quarters authored Jul 30, 2024
2 parents 9f08d86 + 12dde84 commit 3309c28
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 88 deletions.
11 changes: 5 additions & 6 deletions mtop-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ use tracing::instrument::WithSubscriber;
/// Implementation of a `ClientFactory` that creates new Memcached clients that
/// use plaintext or TLS TCP connections.
#[derive(Debug)]
pub struct MemcachedFactory {
pub struct TcpFactory {
client_config: Option<Arc<ClientConfig>>,
server_name: Option<ServerName<'static>>,
}

impl MemcachedFactory {
impl TcpFactory {
pub async fn new(tls: TlsConfig, handle: Handle) -> Result<Self, MtopError> {
let server_name = if tls.enabled { tls.server_name.clone() } else { None };

Expand All @@ -37,7 +37,7 @@ impl MemcachedFactory {
}
}

impl ClientFactory<Server, Memcached> for MemcachedFactory {
impl ClientFactory<Server, Memcached> for TcpFactory {
async fn make(&self, addr: &Server) -> Result<Memcached, MtopError> {
if let Some(cfg) = &self.client_config {
let server_name = self.server_name.clone().unwrap_or_else(|| addr.server_name().clone());
Expand Down Expand Up @@ -223,7 +223,7 @@ impl Default for MemcachedClientConfig {
/// Memcached client that operates on multiple servers, pooling connections
/// to them, and sharding keys via a `Selector` implementation.
#[derive(Debug)]
pub struct MemcachedClient<S, F>
pub struct MemcachedClient<S = SelectorRendezvous, F = TcpFactory>
where
S: Selector + Send + Sync + 'static,
F: ClientFactory<Server, Memcached> + Send + Sync + 'static,
Expand Down Expand Up @@ -690,7 +690,6 @@ mod test {
let client = new_client!(
"cache01.example.com:11211" => "key1" => "STORED\r\n".as_bytes(),
);
let res = client.set("key1", 1, 60, "foo".as_bytes()).await;
let _ = res.unwrap();
client.set("key1", 1, 60, "foo".as_bytes()).await.unwrap();
}
}
3 changes: 1 addition & 2 deletions mtop-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ mod pool;
mod timeout;

pub use crate::client::{
MemcachedClient, MemcachedClientConfig, MemcachedFactory, Selector, SelectorRendezvous, ServersResponse,
ValuesResponse,
MemcachedClient, MemcachedClientConfig, Selector, SelectorRendezvous, ServersResponse, TcpFactory, ValuesResponse,
};
pub use crate::core::{
ErrorKind, Key, Memcached, Meta, MtopError, ProtocolError, ProtocolErrorKind, Slab, SlabItem, SlabItems, Slabs,
Expand Down
6 changes: 3 additions & 3 deletions mtop/src/bench.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mtop_client::{MemcachedClient, MemcachedFactory, MtopError, SelectorRendezvous, Timeout};
use mtop_client::{MemcachedClient, MtopError, Timeout};
use rand::Rng;
use rand_distr::Exp;
use std::fmt;
Expand Down Expand Up @@ -54,7 +54,7 @@ impl fmt::Display for Percent {
/// Spawn one or more workers to perform gets and sets against a Memcached server as fast as possible.
#[derive(Debug)]
pub struct Bencher {
client: Arc<MemcachedClient<SelectorRendezvous, MemcachedFactory>>,
client: Arc<MemcachedClient>,
handle: Handle,
delay: Duration,
timeout: Duration,
Expand All @@ -75,7 +75,7 @@ impl Bencher {
// STFU clippy
#[allow(clippy::too_many_arguments)]
pub fn new(
client: MemcachedClient<SelectorRendezvous, MemcachedFactory>,
client: MemcachedClient,
handle: Handle,
delay: Duration,
timeout: Duration,
Expand Down
78 changes: 16 additions & 62 deletions mtop/src/bin/mc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use mtop::bench::{Bencher, Percent, Summary};
use mtop::check::{Bundle, Checker};
use mtop::profile;
use mtop_client::{
DiscoveryDefault, MemcachedClient, MemcachedClientConfig, MemcachedFactory, Meta, MtopError, SelectorRendezvous,
Server, Timeout, TlsConfig, Value,
DiscoveryDefault, MemcachedClient, MemcachedClientConfig, Meta, MtopError, SelectorRendezvous, Server, TcpFactory,
Timeout, TlsConfig, Value,
};
use rustls_pki_types::{InvalidDnsNameError, ServerName};
use std::path::PathBuf;
Expand Down Expand Up @@ -339,10 +339,7 @@ async fn main() -> ExitCode {
code
}

async fn new_client(
opts: &McConfig,
servers: &[Server],
) -> Result<MemcachedClient<SelectorRendezvous, MemcachedFactory>, MtopError> {
async fn new_client(opts: &McConfig, servers: &[Server]) -> Result<MemcachedClient, MtopError> {
let tls_config = TlsConfig {
enabled: opts.tls_enabled,
ca_path: opts.tls_ca.clone(),
Expand All @@ -356,14 +353,11 @@ async fn new_client(
};

let selector = SelectorRendezvous::new(servers.to_vec());
let factory = MemcachedFactory::new(tls_config, Handle::current()).await?;
let factory = TcpFactory::new(tls_config, Handle::current()).await?;
Ok(MemcachedClient::new(cfg, Handle::current(), selector, factory))
}

async fn connect(
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
timeout: Duration,
) -> Result<(), MtopError> {
async fn connect(client: &MemcachedClient, timeout: Duration) -> Result<(), MtopError> {
let pings = client
.ping()
.timeout(timeout, "client.ping")
Expand All @@ -377,11 +371,7 @@ async fn connect(
Ok(())
}

async fn run_add(
opts: &McConfig,
cmd: &AddCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_add(opts: &McConfig, cmd: &AddCommand, client: &MemcachedClient) -> ExitCode {
let buf = match read_input().await {
Ok(v) => v,
Err(e) => {
Expand All @@ -403,11 +393,7 @@ async fn run_add(
}
}

async fn run_bench(
opts: &McConfig,
cmd: &BenchCommand,
client: MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_bench(opts: &McConfig, cmd: &BenchCommand, client: MemcachedClient) -> ExitCode {
let stop = Arc::new(AtomicBool::new(false));
mtop::sig::wait_for_interrupt(Handle::current(), stop.clone()).await;

Expand All @@ -431,7 +417,7 @@ async fn run_bench(
async fn run_check(
opts: &McConfig,
cmd: &CheckCommand,
client: MemcachedClient<SelectorRendezvous, MemcachedFactory>,
client: MemcachedClient,
resolver: DiscoveryDefault,
) -> ExitCode {
let stop = Arc::new(AtomicBool::new(false));
Expand All @@ -454,11 +440,7 @@ async fn run_check(
}
}

async fn run_decr(
opts: &McConfig,
cmd: &DecrCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_decr(opts: &McConfig, cmd: &DecrCommand, client: &MemcachedClient) -> ExitCode {
if let Err(e) = client
.decr(&cmd.key, cmd.delta)
.timeout(Duration::from_secs(opts.timeout_secs), "client.decr")
Expand All @@ -472,11 +454,7 @@ async fn run_decr(
}
}

async fn run_delete(
opts: &McConfig,
cmd: &DeleteCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_delete(opts: &McConfig, cmd: &DeleteCommand, client: &MemcachedClient) -> ExitCode {
if let Err(e) = client
.delete(&cmd.key)
.timeout(Duration::from_secs(opts.timeout_secs), "client.delete")
Expand All @@ -490,11 +468,7 @@ async fn run_delete(
}
}

async fn run_get(
opts: &McConfig,
cmd: &GetCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_get(opts: &McConfig, cmd: &GetCommand, client: &MemcachedClient) -> ExitCode {
let response = match client
.get(&[cmd.key.clone()])
.timeout(Duration::from_secs(opts.timeout_secs), "client.get")
Expand Down Expand Up @@ -525,11 +499,7 @@ async fn run_get(
}
}

async fn run_incr(
opts: &McConfig,
cmd: &IncrCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_incr(opts: &McConfig, cmd: &IncrCommand, client: &MemcachedClient) -> ExitCode {
if let Err(e) = client
.incr(&cmd.key, cmd.delta)
.timeout(Duration::from_secs(opts.timeout_secs), "client.incr")
Expand All @@ -543,11 +513,7 @@ async fn run_incr(
}
}

async fn run_keys(
opts: &McConfig,
cmd: &KeysCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_keys(opts: &McConfig, cmd: &KeysCommand, client: &MemcachedClient) -> ExitCode {
let response = match client
.metas()
.timeout(Duration::from_secs(opts.timeout_secs), "client.metas")
Expand Down Expand Up @@ -580,11 +546,7 @@ async fn run_keys(
}
}

async fn run_replace(
opts: &McConfig,
cmd: &ReplaceCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_replace(opts: &McConfig, cmd: &ReplaceCommand, client: &MemcachedClient) -> ExitCode {
let buf = match read_input().await {
Ok(v) => v,
Err(e) => {
Expand All @@ -606,11 +568,7 @@ async fn run_replace(
}
}

async fn run_set(
opts: &McConfig,
cmd: &SetCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_set(opts: &McConfig, cmd: &SetCommand, client: &MemcachedClient) -> ExitCode {
let buf = match read_input().await {
Ok(v) => v,
Err(e) => {
Expand All @@ -632,11 +590,7 @@ async fn run_set(
}
}

async fn run_touch(
opts: &McConfig,
cmd: &TouchCommand,
client: &MemcachedClient<SelectorRendezvous, MemcachedFactory>,
) -> ExitCode {
async fn run_touch(opts: &McConfig, cmd: &TouchCommand, client: &MemcachedClient) -> ExitCode {
if let Err(e) = client
.touch(&cmd.key, cmd.ttl)
.timeout(Duration::from_secs(opts.timeout_secs), "client.touch")
Expand Down
17 changes: 5 additions & 12 deletions mtop/src/bin/mtop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::{Parser, ValueHint};
use mtop::queue::{BlockingStatsQueue, Host, StatsQueue};
use mtop::ui::{Theme, TAILWIND};
use mtop_client::{
DiscoveryDefault, MemcachedClient, MemcachedFactory, MtopError, SelectorRendezvous, Server, Timeout, TlsConfig,
DiscoveryDefault, MemcachedClient, MtopError, SelectorRendezvous, Server, TcpFactory, Timeout, TlsConfig,
};
use rustls_pki_types::{InvalidDnsNameError, ServerName};
use std::env;
Expand Down Expand Up @@ -218,10 +218,7 @@ async fn expand_hosts(
Ok(out)
}

async fn new_client(
opts: &MtopConfig,
servers: &[Server],
) -> Result<MemcachedClient<SelectorRendezvous, MemcachedFactory>, MtopError> {
async fn new_client(opts: &MtopConfig, servers: &[Server]) -> Result<MemcachedClient, MtopError> {
let tls_config = TlsConfig {
enabled: opts.tls_enabled,
ca_path: opts.tls_ca.clone(),
Expand All @@ -231,7 +228,7 @@ async fn new_client(
};

let selector = SelectorRendezvous::new(servers.to_vec());
let factory = MemcachedFactory::new(tls_config, Handle::current()).await?;
let factory = TcpFactory::new(tls_config, Handle::current()).await?;
Ok(MemcachedClient::new(
Default::default(),
Handle::current(),
Expand All @@ -242,17 +239,13 @@ async fn new_client(

#[derive(Debug)]
struct UpdateTask {
client: MemcachedClient<SelectorRendezvous, MemcachedFactory>,
client: MemcachedClient,
queue: Arc<StatsQueue>,
timeout: Duration,
}

impl UpdateTask {
fn new(
client: MemcachedClient<SelectorRendezvous, MemcachedFactory>,
queue: Arc<StatsQueue>,
timeout: Duration,
) -> Self {
fn new(client: MemcachedClient, queue: Arc<StatsQueue>, timeout: Duration) -> Self {
UpdateTask { client, queue, timeout }
}

Expand Down
6 changes: 3 additions & 3 deletions mtop/src/check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mtop_client::{DiscoveryDefault, Key, MemcachedClient, MemcachedFactory, SelectorRendezvous, Timeout};
use mtop_client::{DiscoveryDefault, Key, MemcachedClient, Timeout};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
Expand All @@ -10,7 +10,7 @@ const VALUE: &[u8] = "test".as_bytes();
/// Repeatedly make connections to a Memcached server to verify connectivity.
#[derive(Debug)]
pub struct Checker {
client: MemcachedClient<SelectorRendezvous, MemcachedFactory>,
client: MemcachedClient,
resolver: DiscoveryDefault,
delay: Duration,
timeout: Duration,
Expand All @@ -23,7 +23,7 @@ impl Checker {
/// part of the test may take (DNS resolution, connecting, setting a value, and fetching
/// a value).
pub fn new(
client: MemcachedClient<SelectorRendezvous, MemcachedFactory>,
client: MemcachedClient,
resolver: DiscoveryDefault,
delay: Duration,
timeout: Duration,
Expand Down

0 comments on commit 3309c28

Please sign in to comment.