Skip to content

Commit

Permalink
feat(api): parse entry cache flags as duration
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 29, 2024
1 parent fac0887 commit 3906749
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
4 changes: 4 additions & 0 deletions crates/synd_api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Following metrics are exported
| `http.server.request` | Counter | http request traffic per status code |
| `graphql.duration` | Histogram | graphql latency |
| `usecase` | Counter | usecase traffic per operation |
| `cache.feed.count` | Gauge | feed cache entry count |
| `cache.feed.size` | Gauge | feed cache size |


## Configurations
Expand All @@ -39,6 +41,8 @@ Following metrics are exported
| `--show-code-location` | Show code location(foo.rs:10) in signals(logs) | `false` |
| `--show-target` | Show tracing target(module) ins signals(logs) | `true` |
| `--trace-sampler-ratio` | Trace sampler ratio | `1` |
| `--feed-cache-ttl` | Feed entry cache TTL | `180min` |
| `-feed-cache-refresh-interval` | Feed entry cache refresh interval | `120min` |


## Features
Expand Down
10 changes: 5 additions & 5 deletions crates/synd_api/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ pub struct ObservabilityOptions {
pub trace_sampler_ratio: f64,
}

#[derive(clap::Args, Debug)]
#[derive(clap::Args, Debug, Clone)]
#[command(next_help_heading = "Cache options")]
pub struct CacheOptions {
/// Max feed cache size in MiB
#[arg(long, default_value_t = config::cache::DEFAULT_FEED_CACHE_SIZE_MB, env = env_key!("FEED_CACHE_SIZE") )]
pub feed_cache_size_mb: u64,
#[arg(long, default_value_t = config::cache::DEFAULT_FEED_CACHE_TTL_MINUTES, env = env_key!("FEED_CACHE_TTL_MINUTES"))]
pub feed_cache_ttl_minutes: u64,
#[arg(long, default_value_t = config::cache::DEFAULT_FEED_CACHE_REFRESH_INTERVAL_MINUTES, env = env_key!("FEED_CACHE_REFRESH_INTERVAL_MINUTES"))]
pub feed_cache_refresh_interval_minutes: u64,
#[arg(long, value_parser = parse_duration::parse, default_value = config::cache::DEFAULT_FEED_CACHE_TTL, env = env_key!("FEED_CACHE_TTL"))]
pub feed_cache_ttl: Duration,
#[arg(long, value_parser = parse_duration::parse, default_value = config::cache::DEFAULT_FEED_CACHE_REFRESH_INTERVAL, env = env_key!("FEED_CACHE_REFRESH_INTERVAL"))]
pub feed_cache_refresh_interval: Duration,
}

#[must_use]
Expand Down
4 changes: 2 additions & 2 deletions crates/synd_api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ pub mod metrics {

pub mod cache {
pub const DEFAULT_FEED_CACHE_SIZE_MB: u64 = 100;
pub const DEFAULT_FEED_CACHE_TTL_MINUTES: u64 = 180;
pub const DEFAULT_FEED_CACHE_REFRESH_INTERVAL_MINUTES: u64 = 120;
pub const DEFAULT_FEED_CACHE_TTL: &str = "180min";
pub const DEFAULT_FEED_CACHE_REFRESH_INTERVAL: &str = "120min";
}
10 changes: 4 additions & 6 deletions crates/synd_api/src/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,21 @@ impl Dependency {
let cache_feed_service = {
let CacheOptions {
feed_cache_size_mb,
feed_cache_ttl_minutes,
feed_cache_refresh_interval_minutes,
feed_cache_ttl,
feed_cache_refresh_interval,
} = cache;
let feed_service = FeedService::new(config::USER_AGENT, 10 * 1024 * 1024);
let cache_feed_service = CacheLayer::with(
feed_service,
CacheConfig::default()
.with_max_cache_size(feed_cache_size_mb * 1024 * 1024)
.with_time_to_live(Duration::from_secs(feed_cache_ttl_minutes * 60)),
.with_time_to_live(feed_cache_ttl),
);
let periodic_refresher = cache_feed_service
.periodic_refresher()
.with_emit_metrics(true);

tokio::spawn(periodic_refresher.run(Duration::from_secs(
feed_cache_refresh_interval_minutes * 60,
)));
tokio::spawn(periodic_refresher.run(feed_cache_refresh_interval));

cache_feed_service
};
Expand Down
4 changes: 3 additions & 1 deletion crates/synd_api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ async fn run(
}: Args,
shutdown: Shutdown,
) -> anyhow::Result<()> {
let dep = Dependency::new(kvsd, tls, serve, cache).await?;
let dep = Dependency::new(kvsd, tls, serve, cache.clone()).await?;

info!(
version = config::VERSION,
otlp_endpoint=?o11y.otlp_endpoint,
request_timeout=?dep.serve_options.timeout,
request_body_limit_bytes=dep.serve_options.body_limit_bytes,
concurrency_limit=?dep.serve_options.concurrency_limit,
feed_cache_ttl_minutes=?cache.feed_cache_ttl.as_secs() / 60,
feed_cache_refresh_interval_minutes=?cache.feed_cache_refresh_interval.as_secs() / 60,
"Runinng...",
);

Expand Down

0 comments on commit 3906749

Please sign in to comment.