Skip to content

Commit

Permalink
Rename linkerd-cache to linkerd-idle-cache (#2118)
Browse files Browse the repository at this point in the history
We have multiple types of caching middlewares. The `linkerd-cache`
middleware has the specific behavior that it implements idle eviction.
This change renames the module and types to reflect this distinction.
  • Loading branch information
olix0r authored Jan 5, 2023
1 parent b29bd8d commit 5d0b226
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 55 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,6 @@ dependencies = [
"hyper",
"ipnet",
"linkerd-addr",
"linkerd-cache",
"linkerd-conditional",
"linkerd-detect",
"linkerd-dns",
Expand All @@ -795,6 +794,7 @@ dependencies = [
"linkerd-http-classify",
"linkerd-http-metrics",
"linkerd-identity",
"linkerd-idle-cache",
"linkerd-io",
"linkerd-meshtls",
"linkerd-metrics",
Expand Down Expand Up @@ -864,8 +864,8 @@ dependencies = [
"libfuzzer-sys",
"linkerd-app-core",
"linkerd-app-test",
"linkerd-cache",
"linkerd-http-access-log",
"linkerd-idle-cache",
"linkerd-io",
"linkerd-meshtls",
"linkerd-meshtls-rustls",
Expand Down Expand Up @@ -966,20 +966,6 @@ dependencies = [
"tracing-subscriber",
]

[[package]]
name = "linkerd-cache"
version = "0.1.0"
dependencies = [
"futures",
"linkerd-error",
"linkerd-stack",
"linkerd-tracing",
"parking_lot",
"tokio",
"tower",
"tracing",
]

[[package]]
name = "linkerd-conditional"
version = "0.1.0"
Expand Down Expand Up @@ -1178,6 +1164,20 @@ dependencies = [
"linkerd-error",
]

[[package]]
name = "linkerd-idle-cache"
version = "0.1.0"
dependencies = [
"futures",
"linkerd-error",
"linkerd-stack",
"linkerd-tracing",
"parking_lot",
"tokio",
"tower",
"tracing",
]

[[package]]
name = "linkerd-io"
version = "0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ members = [
"linkerd/app/outbound",
"linkerd/app/test",
"linkerd/app",
"linkerd/cache",
"linkerd/conditional",
"linkerd/distribute",
"linkerd/detect",
Expand All @@ -32,6 +31,7 @@ members = [
"linkerd/http-retry",
"linkerd/http-route",
"linkerd/identity",
"linkerd/idle-cache",
"linkerd/io",
"linkerd/meshtls",
"linkerd/meshtls/boring",
Expand Down
13 changes: 3 additions & 10 deletions linkerd/app/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ independently of the inbound and outbound proxy logic.
"""

[dependencies]
linkerd-conditional = { path = "../../conditional" }
bytes = "1"
drain = { version = "0.1", features = ["retain"] }
http = "0.2"
Expand All @@ -21,8 +22,6 @@ hyper = { version = "0.14", features = ["http1", "http2"] }
futures = { version = "0.3", default-features = false }
ipnet = "2.7"
linkerd-addr = { path = "../../addr" }
linkerd-cache = { path = "../../cache" }
linkerd-conditional = { path = "../../conditional" }
linkerd-dns = { path = "../../dns" }
linkerd-detect = { path = "../../detect" }
linkerd-duplex = { path = "../../duplex" }
Expand All @@ -33,6 +32,7 @@ linkerd-exp-backoff = { path = "../../exp-backoff" }
linkerd-http-classify = { path = "../../http-classify" }
linkerd-http-metrics = { path = "../../http-metrics" }
linkerd-identity = { path = "../../identity" }
linkerd-idle-cache = { path = "../../idle-cache" }
linkerd-io = { path = "../../io" }
linkerd-meshtls = { path = "../../meshtls", default-features = false }
linkerd-metrics = { path = "../../metrics", features = ["linkerd-stack"] }
Expand Down Expand Up @@ -71,14 +71,7 @@ pin-project = "1"
[dependencies.tower]
version = "0.4"
default-features = false
features = [
"buffer",
"make",
"spawn-ready",
"timeout",
"util",
"limit",
]
features = ["buffer", "make", "spawn-ready", "timeout", "util", "limit"]

[target.'cfg(target_os = "linux")'.dependencies]
linkerd-system = { path = "../../system" }
Expand Down
2 changes: 1 addition & 1 deletion linkerd/app/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
pub use drain;
pub use ipnet::{IpNet, Ipv4Net, Ipv6Net};
pub use linkerd_addr::{self as addr, Addr, NameAddr};
pub use linkerd_cache as cache;
pub use linkerd_conditional::Conditional;
pub use linkerd_detect as detect;
pub use linkerd_dns;
pub use linkerd_error::{cause_ref, is_caused_by, Error, Infallible, Recover, Result};
pub use linkerd_exp_backoff as exp_backoff;
pub use linkerd_http_metrics as http_metrics;
pub use linkerd_idle_cache as idle_cache;
pub use linkerd_io as io;
pub use linkerd_opencensus as opencensus;
pub use linkerd_service_profiles as profiles;
Expand Down
6 changes: 3 additions & 3 deletions linkerd/app/core/src/svc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Possibly unused, but useful during development.

pub use crate::proxy::http;
use crate::{cache, config::BufferConfig, Error};
use crate::{config::BufferConfig, idle_cache, Error};
use linkerd_error::Recover;
use linkerd_exp_backoff::{ExponentialBackoff, ExponentialBackoffStream};
pub use linkerd_reconnect::NewReconnect;
Expand Down Expand Up @@ -222,13 +222,13 @@ impl<S> Stack<S> {
self.push_on_service(buffer(name, config))
}

pub fn push_cache<T>(self, idle: Duration) -> Stack<cache::NewCachedService<T, S>>
pub fn push_cache<T>(self, idle: Duration) -> Stack<idle_cache::NewIdleCached<T, S>>
where
T: Clone + Eq + std::fmt::Debug + std::hash::Hash + Send + Sync + 'static,
S: NewService<T> + 'static,
S::Service: Send + Sync + 'static,
{
self.push(cache::NewCachedService::layer(idle))
self.push(idle_cache::NewIdleCached::layer(idle))
}

/// Push a service that either calls the inner service if it is ready, or
Expand Down
4 changes: 2 additions & 2 deletions linkerd/app/inbound/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ bytes = "1"
http = "0.2"
futures = { version = "0.3", default-features = false }
linkerd-app-core = { path = "../core" }
linkerd-cache = { path = "../../cache" }
linkerd-http-access-log = { path = "../../http-access-log" }
linkerd-idle-cache = { path = "../../idle-cache" }
linkerd-server-policy = { path = "../../server-policy", features = ["proto"] }
linkerd-tonic-watch = { path = "../../tonic-watch" }
linkerd2-proxy-api = { version = "0.7", features = ["inbound"] }
Expand All @@ -36,7 +36,7 @@ libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }
[dev-dependencies]
hyper = { version = "0.14", features = ["http1", "http2"] }
linkerd-app-test = { path = "../test" }
linkerd-cache = { path = "../../cache", features = ["test-util"] }
linkerd-idle-cache = { path = "../../idle-cache", features = ["test-util"] }
linkerd-io = { path = "../../io", features = ["tokio-test"] }
linkerd-meshtls = { path = "../../meshtls", features = ["rustls"] }
linkerd-meshtls-rustls = { path = "../../meshtls/rustls", features = [
Expand Down
6 changes: 4 additions & 2 deletions linkerd/app/inbound/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ libfuzzer-sys = { version = "0.4", features = ["arbitrary-derive"] }
linkerd-app-core = { path = "../../core" }
linkerd-app-inbound = { path = ".." }
linkerd-app-test = { path = "../../test" }
linkerd-cache = { path = "../../../cache", features = ["test-util"] }
linkerd-idle-cache = { path = "../../../idle-cache", features = ["test-util"] }
linkerd-meshtls = { path = "../../../meshtls", features = ["rustls"] }
linkerd-meshtls-rustls = { path = "../../../meshtls/rustls", features = ["test-util"] }
linkerd-meshtls-rustls = { path = "../../../meshtls/rustls", features = [
"test-util",
] }
linkerd-tracing = { path = "../../../tracing", features = ["ansi"] }
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
Expand Down
2 changes: 1 addition & 1 deletion linkerd/app/inbound/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use linkerd_app_core::{
tls,
transport::{ClientAddr, OrigDstAddr, Remote},
};
use linkerd_cache::Cached;
use linkerd_idle_cache::Cached;
pub use linkerd_server_policy::{
authz::Suffix,
grpc::Route as GrpcRoute,
Expand Down
8 changes: 4 additions & 4 deletions linkerd/app/inbound/src/policy/store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{api, AllowPolicy, DefaultPolicy, GetPolicy};
use linkerd_app_core::{proxy::http, transport::OrigDstAddr, Error};
use linkerd_cache::Cache;
use linkerd_idle_cache::IdleCache;
pub use linkerd_server_policy::{
authz::Suffix, Authentication, Authorization, Protocol, ServerPolicy,
};
Expand All @@ -13,7 +13,7 @@ use tracing::info_span;

#[derive(Clone)]
pub struct Store<S> {
cache: Cache<u16, Rx, BuildHasherDefault<PortHasher>>,
cache: IdleCache<u16, Rx, BuildHasherDefault<PortHasher>>,
default_rx: Rx,
discover: Option<api::Watch<S>>,
}
Expand Down Expand Up @@ -43,7 +43,7 @@ impl<S> Store<S> {
let (_, rx) = watch::channel(s);
(p, rx)
});
Cache::with_permanent_from_iter(idle_timeout, rxs)
IdleCache::with_permanent_from_iter(idle_timeout, rxs)
};

Self {
Expand Down Expand Up @@ -83,7 +83,7 @@ impl<S> Store<S> {
.in_scope(|| discover.spawn_with_init(port, default.into()));
(port, rx)
});
Cache::with_permanent_from_iter(idle_timeout, rxs)
IdleCache::with_permanent_from_iter(idle_timeout, rxs)
};

Self {
Expand Down
14 changes: 11 additions & 3 deletions linkerd/cache/Cargo.toml → linkerd/idle-cache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "linkerd-cache"
name = "linkerd-idle-cache"
version = "0.1.0"
authors = ["Linkerd Developers <[email protected]>"]
license = "Apache-2.0"
Expand All @@ -14,10 +14,18 @@ futures = { version = "0.3", default-features = false }
linkerd-error = { path = "../error" }
linkerd-stack = { path = "../stack" }
parking_lot = "0.12"
tokio = { version = "1", default-features = false, features = ["macros", "rt", "sync", "time"] }
tokio = { version = "1", default-features = false, features = [
"macros",
"rt",
"sync",
"time",
] }
tower = { version = "0.4", default-features = false, features = ["util"] }
tracing = "0.1"

[dev-dependencies]
tokio = { version = "1", default-features = false, features = ["test-util", "time"] }
tokio = { version = "1", default-features = false, features = [
"test-util",
"time",
] }
linkerd-tracing = { path = "../tracing", features = ["ansi"] }
24 changes: 18 additions & 6 deletions linkerd/cache/src/lib.rs → linkerd/idle-cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use tracing::{debug, instrument, trace};

mod new_service;

pub use new_service::NewCachedService;
pub use new_service::NewIdleCached;

#[derive(Clone)]
pub struct Cache<K, V, S = RandomState>
pub struct IdleCache<K, V, S = RandomState>
where
K: Eq + Hash,
{
Expand Down Expand Up @@ -61,7 +61,7 @@ type InnerMap<K, V, S> = RwLock<HashMap<K, CacheEntry<V>, S>>;

// === impl Cache ===

impl<K, V> Cache<K, V>
impl<K, V> IdleCache<K, V>
where
K: Clone + std::fmt::Debug + Eq + Hash + Send + Sync + 'static,
V: Send + Sync + 'static,
Expand All @@ -71,7 +71,7 @@ where
}
}

impl<K, V, S> Cache<K, V, BuildHasherDefault<S>>
impl<K, V, S> IdleCache<K, V, BuildHasherDefault<S>>
where
K: Clone + std::fmt::Debug + Eq + Hash + Send + Sync + 'static,
V: Send + Sync + 'static,
Expand Down Expand Up @@ -106,7 +106,7 @@ where
}
}

impl<K, V, S> Cache<K, V, S>
impl<K, V, S> IdleCache<K, V, S>
where
K: Clone + std::fmt::Debug + Eq + Hash + Send + Sync + 'static,
V: Send + Sync + 'static,
Expand Down Expand Up @@ -329,6 +329,18 @@ impl<V> Cached<V> {
}
}

impl<T, N> linkerd_stack::NewService<T> for Cached<N>
where
N: linkerd_stack::NewService<T> + Send + Sync + 'static,
{
type Service = N::Service;

#[inline]
fn new_service(&self, target: T) -> Self::Service {
self.inner.new_service(target)
}
}

impl<Req, S> tower::Service<Req> for Cached<S>
where
S: tower::Service<Req> + Send + Sync + 'static,
Expand Down Expand Up @@ -375,7 +387,7 @@ async fn test_idle_retain() {
time::pause();

let idle = time::Duration::from_secs(10);
let cache = Cache::new(idle);
let cache = IdleCache::new(idle);

let handle = cache.spawn_idle(());
let weak = Arc::downgrade(&handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use super::*;
use linkerd_stack::{layer, NewService};

#[derive(Clone)]
pub struct NewCachedService<T, N>
pub struct NewIdleCached<T, N>
where
T: Eq + Hash,
N: NewService<T>,
{
cache: Cache<T, N::Service>,
cache: IdleCache<T, N::Service>,
new_svc: N,
}

// === impl NewCachedService ===
// === impl NewIdleCached ===

impl<T, N> NewCachedService<T, N>
impl<T, N> NewIdleCached<T, N>
where
T: Clone + std::fmt::Debug + Eq + Hash + Send + Sync + 'static,
N: NewService<T> + 'static,
Expand All @@ -22,12 +22,12 @@ where
pub fn layer(idle: time::Duration) -> impl layer::Layer<N, Service = Self> + Clone {
layer::mk(move |new_svc| Self {
new_svc,
cache: Cache::new(idle),
cache: IdleCache::new(idle),
})
}
}

impl<T, N> NewService<T> for NewCachedService<T, N>
impl<T, N> NewService<T> for NewIdleCached<T, N>
where
T: Clone + std::fmt::Debug + Eq + Hash + Send + Sync + 'static,
N: NewService<T> + 'static,
Expand Down

0 comments on commit 5d0b226

Please sign in to comment.