Skip to content

Conversation

@abtreece
Copy link
Owner

Summary

  • Adds TTL-based caching for os.Stat() results in the template cache to reduce syscall overhead
  • Configurable via --stat-cache-ttl CLI flag (default: 1s) or stat_cache_ttl TOML option
  • New metric confd_template_stat_cache_hits_total tracks how often stat syscalls are skipped
  • Setting TTL to 0 disables the cache and always performs stat (previous behavior)

Test plan

  • Unit tests pass: go test ./pkg/template/... -run TestStatCacheTTL -v
  • All existing tests updated and passing
  • Build succeeds: make build
  • CLI flag appears in help: ./bin/confd --help | grep stat-cache-ttl

Closes #446

Copilot AI review requested due to automatic review settings January 22, 2026 04:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds TTL-based caching for os.Stat() calls on template files to reduce syscall overhead. When a cached template is accessed within the configured TTL window, the stat check is skipped, assuming the file hasn't changed. This optimization is particularly beneficial in high-frequency template rendering scenarios.

Changes:

  • Added statTime field to track when file stats were last verified, with configurable TTL (default 1s)
  • Introduced --stat-cache-ttl CLI flag and stat_cache_ttl TOML option for configuration
  • Added confd_template_stat_cache_hits_total metric to monitor stat cache effectiveness

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/template/template_cache.go Implements stat caching logic with TTL checks and statTime tracking
pkg/template/template_cache_test.go Updates all test initialization calls and adds comprehensive TTL behavior tests
pkg/metrics/metrics.go Adds new counter metric for tracking stat cache hits
cmd/confd/config.go Adds TOML configuration parsing for stat_cache_ttl
cmd/confd/cli.go Adds CLI flag and default constant for stat cache TTL

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}
if enabled {
log.Debug("Template cache enabled")
log.Debug("Template cache enabled with stat TTL %v", statTTL)
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log.Debug call is missing a format specifier. It should use string formatting (e.g., log.Debugf) or concatenate the value properly to avoid treating statTTL as a format argument.

Suggested change
log.Debug("Template cache enabled with stat TTL %v", statTTL)
log.Debugf("Template cache enabled with stat TTL %v", statTTL)

Copilot uses AI. Check for mistakes.
Comment on lines +137 to +140
if d, err := time.ParseDuration(tomlCfg.StatCacheTTL); err == nil {
if cli.StatCacheTTL == DefaultStatCacheTTL {
cli.StatCacheTTL = d
}
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When time.ParseDuration fails, the error is silently ignored. Users won't know their configuration is invalid. Consider logging a warning or returning an error when the stat_cache_ttl value cannot be parsed.

Suggested change
if d, err := time.ParseDuration(tomlCfg.StatCacheTTL); err == nil {
if cli.StatCacheTTL == DefaultStatCacheTTL {
cli.StatCacheTTL = d
}
d, err := time.ParseDuration(tomlCfg.StatCacheTTL)
if err != nil {
log.Debug("Invalid stat_cache_ttl %q in config file %s: %v; using default %s", tomlCfg.StatCacheTTL, cli.ConfigFile, err, DefaultStatCacheTTL)
} else if cli.StatCacheTTL == DefaultStatCacheTTL {
cli.StatCacheTTL = d

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Jan 22, 2026

Codecov Report

❌ Patch coverage is 75.75758% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.00%. Comparing base (57ad7f2) to head (72b823c).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
cmd/confd/config.go 0.00% 3 Missing and 1 partial ⚠️
pkg/template/template_cache.go 85.00% 2 Missing and 1 partial ⚠️
cmd/confd/cli.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #458      +/-   ##
==========================================
+ Coverage   59.85%   60.00%   +0.14%     
==========================================
  Files          48       48              
  Lines        5244     5273      +29     
==========================================
+ Hits         3139     3164      +25     
- Misses       1899     1901       +2     
- Partials      206      208       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add configurable TTL for caching os.Stat() results in the template cache.
This reduces syscall overhead when templates are processed frequently,
such as during batch processing or rapid watch mode updates.

- Add statTime field to cachedTemplate to track last stat verification
- Add statTTL to TemplateCache (default: 1s, 0 disables)
- Add --stat-cache-ttl CLI flag and stat_cache_ttl TOML config option
- Add confd_template_stat_cache_hits_total metric to track skipped stats
- Update InitTemplateCache signature to accept statTTL parameter

Closes #446
@abtreece abtreece force-pushed the perf/stat-cache-ttl branch from 8e6591b to 72b823c Compare January 22, 2026 04:28
@abtreece abtreece merged commit 7fbc5a0 into main Jan 22, 2026
11 checks passed
@abtreece abtreece deleted the perf/stat-cache-ttl branch January 22, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add TTL-based caching for template file stat checks

1 participant