Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce feature unstable-client-cache to make the client cache optional. #478

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ tsig = ["bytes", "ring", "smallvec"]
zonefile = ["bytes", "serde", "std"]

# Unstable features
unstable-client-transport = ["moka", "net", "tracing"]
unstable-client-transport = ["net", "tracing"]
unstable-client-cache = ["unstable-client-transport", "moka"]
unstable-server-transport = ["arc-swap", "chrono/clock", "libc", "net", "siphasher", "tracing"]
unstable-sign = ["std", "dep:secrecy", "unstable-validate", "time/formatting"]
unstable-stelline = ["tokio/test-util", "tracing", "tracing-subscriber", "tsig", "unstable-client-transport", "unstable-server-transport", "zonefile"]
unstable-validate = ["bytes", "std", "ring"]
unstable-validator = ["unstable-validate", "zonefile", "unstable-client-transport"]
unstable-validator = ["unstable-validate", "zonefile", "unstable-client-transport", "moka"]
unstable-xfr = ["net"]
unstable-zonetree = ["futures-util", "parking_lot", "rustversion", "serde", "std", "tokio", "tracing", "unstable-xfr", "zonefile"]

Expand Down
65 changes: 42 additions & 23 deletions examples/client-transports.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Using the `domain::net::client` module for sending a query.
use domain::base::{MessageBuilder, Name, Rtype};
#[cfg(feature = "unstable-client-cache")]
use domain::net::client::cache;
use domain::net::client::protocol::{TcpConnect, TlsConnect, UdpConnect};
use domain::net::client::request::{
RequestMessage, RequestMessageMulti, SendRequest,
};
use domain::net::client::{
cache, dgram, dgram_stream, load_balancer, multi_stream, redundant,
stream,
dgram, dgram_stream, load_balancer, multi_stream, redundant, stream,
};
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
Expand Down Expand Up @@ -92,27 +93,8 @@ async fn main() {
// when it is no longer needed.
drop(request);

// Create a cached transport.
let mut cache_config = cache::Config::new();
cache_config.set_max_cache_entries(100); // Just an example.
let cache =
cache::Connection::with_config(udptcp_conn.clone(), cache_config);

// Send a request message.
let mut request = cache.send_request(req.clone());

// Get the reply
println!("Wating for cache reply");
let reply = request.get_response().await;
println!("Cache reply: {reply:?}");

// Send the request message again.
let mut request = cache.send_request(req.clone());

// Get the reply
println!("Wating for cached reply");
let reply = request.get_response().await;
println!("Cached reply: {reply:?}");
#[cfg(feature = "unstable-client-cache")]
do_client_cache(udptcp_conn.clone(), req.clone()).await;

#[cfg(feature = "unstable-validator")]
do_validator(udptcp_conn.clone(), req.clone()).await;
Expand Down Expand Up @@ -314,6 +296,43 @@ async fn main() {
}
}

#[cfg(feature = "unstable-client-cache")]
async fn do_client_cache<Octs, SR>(conn: SR, req: RequestMessage<Octs>)
where
Octs: AsRef<[u8]>
+ Clone
+ std::fmt::Debug
+ domain::dep::octseq::Octets
+ domain::dep::octseq::OctetsFrom<Vec<u8>>
+ Send
+ Sync
+ 'static,
<Octs as domain::dep::octseq::OctetsFrom<Vec<u8>>>::Error:
std::fmt::Debug,
SR: Clone + SendRequest<RequestMessage<Octs>> + Send + Sync + 'static,
{
// Create a cached transport.
let mut cache_config = cache::Config::new();
cache_config.set_max_cache_entries(100); // Just an example.
let cache = cache::Connection::with_config(conn, cache_config);

// Send a request message.
let mut request = cache.send_request(req.clone());

// Get the reply
println!("Wating for cache reply");
let reply = request.get_response().await;
println!("Cache reply: {reply:?}");

// Send the request message again.
let mut request = cache.send_request(req.clone());

// Get the reply
println!("Wating for cached reply");
let reply = request.get_response().await;
println!("Cached reply: {reply:?}");
}

#[cfg(feature = "unstable-validator")]
async fn do_validator<Octs, SR>(conn: SR, req: RequestMessage<Octs>)
where
Expand Down
14 changes: 12 additions & 2 deletions src/net/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
//! transport connections. The [load_balancer] transport favors connections
//! with the shortest outstanding request queue. Any of the other transports
//! can be added as upstream transports.
//! * [cache] This is a simple message cache provided as a pass through
#![cfg_attr(feature = "unstable-client-cache", doc = "* [cache]:")]
#![cfg_attr(not(feature = "unstable-client-cache",), doc = "* cache:")]
//! This is a simple message cache provided as a pass through
//! transport. The cache works with any of the other transports.
#![cfg_attr(feature = "tsig", doc = "* [tsig]:")]
#![cfg_attr(not(feature = "tsig",), doc = "* tsig:")]
Expand Down Expand Up @@ -203,7 +205,14 @@
//! * The [multi_stream] transport does not support timeouts or other limits on
//! the number of attempts to open a connection. The caller has to
//! implement a timeout mechanism.
//! * The [cache] transport does not support:
#![cfg_attr(
feature = "unstable-client-cache",
doc = "* The [cache] transport does not support:"
)]
#![cfg_attr(
not(feature = "unstable-client-cache"),
doc = "* The cache transport does not support:"
)]
//! * Prefetching. In this context, prefetching means updating a cache entry
//! before it expires.
//! * [RFC 8767](https://tools.ietf.org/html/rfc8767)
Expand All @@ -223,6 +232,7 @@
#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]

#[cfg(feature = "unstable-client-cache")]
pub mod cache;
pub mod dgram;
pub mod dgram_stream;
Expand Down
Loading