Skip to content

Commit

Permalink
set a default TTL for query plans
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Feb 2, 2024
1 parent d47f266 commit 568fb60
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
73 changes: 72 additions & 1 deletion apollo-router/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ impl Default for Apq {
#[serde(deny_unknown_fields, default)]
pub(crate) struct QueryPlanning {
/// Cache configuration
pub(crate) cache: Cache,
pub(crate) cache: QueryPlanCache,
/// Warms up the cache on reloads by running the query plan over
/// a list of the most used queries (from the in memory cache)
/// Configures the number of queries warmed up. Defaults to 1/3 of
Expand Down Expand Up @@ -880,6 +880,54 @@ pub(crate) struct QueryPlanning {
pub(crate) experimental_paths_limit: Option<u32>,
}

/// Cache configuration
#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields, default)]
pub(crate) struct QueryPlanCache {
/// Configures the in memory cache (always active)
pub(crate) in_memory: InMemoryCache,
/// Configures and activates the Redis cache
pub(crate) redis: Option<QueryPlanRedisCache>,
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
/// Redis cache configuration
pub(crate) struct QueryPlanRedisCache {
/// List of URLs to the Redis cluster
pub(crate) urls: Vec<url::Url>,

/// Redis username if not provided in the URLs. This field takes precedence over the username in the URL
pub(crate) username: Option<String>,
/// Redis password if not provided in the URLs. This field takes precedence over the password in the URL
pub(crate) password: Option<String>,

#[serde(deserialize_with = "humantime_serde::deserialize", default)]
#[schemars(with = "Option<String>", default)]
/// Redis request timeout (default: 2ms)
pub(crate) timeout: Option<Duration>,

#[serde(
deserialize_with = "humantime_serde::deserialize",
default = "default_query_plan_cache_ttl"
)]
#[schemars(with = "Option<String>", default = "default_query_plan_cache_ttl")]
/// TTL for entries
pub(crate) ttl: Duration,

/// namespace used to prefix Redis keys
pub(crate) namespace: Option<String>,

#[serde(default)]
/// TLS client configuration
pub(crate) tls: Option<TlsClient>,
}

fn default_query_plan_cache_ttl() -> Duration {
// Default TTL set to 30 days
Duration::from_secs(86400 * 30)
}

/// Cache configuration
#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields, default)]
Expand All @@ -890,6 +938,15 @@ pub(crate) struct Cache {
pub(crate) redis: Option<RedisCache>,
}

impl From<QueryPlanCache> for Cache {
fn from(value: QueryPlanCache) -> Self {
Cache {
in_memory: value.in_memory,
redis: value.redis.map(Into::into),
}
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
/// In memory cache configuration
Expand Down Expand Up @@ -936,6 +993,20 @@ pub(crate) struct RedisCache {
pub(crate) tls: Option<TlsClient>,
}

impl From<QueryPlanRedisCache> for RedisCache {
fn from(value: QueryPlanRedisCache) -> Self {
RedisCache {
urls: value.urls,
username: value.username,
password: value.password,
timeout: value.timeout,
ttl: Some(value.ttl),
namespace: value.namespace,
tls: value.tls,
}
}
}

/// TLS related configuration options.
#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,10 @@ expression: "&schema"
},
"ttl": {
"description": "TTL for entries",
"default": null,
"default": {
"secs": 2592000,
"nanos": 0
},
"type": "string",
"nullable": true
},
Expand Down
2 changes: 1 addition & 1 deletion apollo-router/src/query_planner/caching_query_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
) -> CachingQueryPlanner<T> {
let cache = Arc::new(
DeduplicatingCache::from_configuration(
&configuration.supergraph.query_planning.cache,
&configuration.supergraph.query_planning.cache.clone().into(),
"query planner",
)
.await,
Expand Down

0 comments on commit 568fb60

Please sign in to comment.