Skip to content

feat: background template reloading p3 - add background reloading - #2153

Merged
cstockton merged 4 commits into
cs/background-template-reloading-p2from
cs/background-template-reloading-p3
Sep 11, 2025
Merged

cstockton merged 4 commits into
cs/background-template-reloading-p2from
cs/background-template-reloading-p3

Conversation

@cstockton

Copy link
Copy Markdown
Contributor

Summary
This PR builds on auth#2150 to deliver phase 3: background template
reloading
. Templates are proactively refreshed in the background while the
request path serves from cache. On failures we keep serving last-good entries or
fall back to built-in defaults. This further reduces latency variance, removes
blocking fetches on hot paths, and preserves current behavior.

Background reloader

  • Introduce a reloader worker in templatemailer that periodically refreshes cached templates.
  • Never evict a template before a replacement is ready. Continue serving the last-good on fetch/parse errors and fall back to defaults if none exist.
  • Enforce HTTP 200 and cap response size when fetching templates.
  • Collapse concurrent refreshes with singleflight (same as cache misses).

Hot-reload & wiring

  • Add FromConfig builder and Reload/ReloadConfig hooks.
  • Server composes the client chain via FromConfig, starts Work() when enabled, and triggers reloads on config changes (no request downtime).
  • Headers logic remains in the mailer, including $messageType substitution and legacy "reauthenticate" compatibility.

Configuration knobs

  • TemplateMaxSize – maximum template body size to fetch and parse.
  • TemplateMaxAge – cache TTL before an entry is considered stale.
  • TemplateRetryIval – interval used to recheck/retry stale entries.
  • TemplateReloadingEnabled – gate to start the background worker.
  • TemplateReloadingMaxIdle – pause background work after this period of inactivity, resume on cache "touch" from incoming requests.

Interfaces & compatibility

  • No changes beyond phase 2: clients still receive pre-rendered subject and body. Task payloads unchanged from the prior PR.
  • API continues to hold a persistent mailer via WithMailer
  • No user-facing API changes, behavior intentionally preserved.

* add reloader worker to templatemailer with periodic refresh; keeps
  serving last-good entries on fetch/parse errors and falls back to
  built-in defaults; never drops an entry before an update is ready
* introduce FromConfig builder plus Reload and ReloadConfig; server
  builds via FromConfig, starts Work() when enabled, and triggers
  reloads on config changes
* add config knobs: TemplateMaxSize, TemplateMaxAge, TemplateRetryIval,
  TemplateReloadingEnabled, TemplateReloadingMaxIdle
* implement idle pause/resume: reloader sleeps after a quiet period and
  resumes on cache touch from mail() paths.
* enforce HTTP 200 and cap body size when fetching templates
* no client changes beyond phase 2, written to preserve existing behavior
@cstockton
cstockton requested a review from a team as a code owner September 3, 2025 00:00
@hf

hf commented Sep 3, 2025

Copy link
Copy Markdown
Contributor

TemplateRetryIval

Let's use Interval instead of Ival

Chris Stockton added 2 commits September 3, 2025 14:47
* Addresses feedback Ival -> Interval
* Eagerly reload templates in work loop instead of waiting on tr
* Previous PR persisted the mailer, which meant mailer.Client
  would not have been updated from live config reloads.
* Previous PR did not update the ticker interval in Work.
* Cleaner scoping of config allows removal of sync in templatemailer
@coveralls

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 17447151393

Details

  • 52 of 195 (26.67%) changed or added relevant lines in 4 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.5%) to 70.38%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/api/apiworker/apiworker.go 0 57 0.0%
internal/mailer/templatemailer/template.go 42 128 32.81%
Files with Coverage Reduction New Missed Lines %
internal/mailer/templatemailer/template.go 2 49.74%
Totals Coverage Status
Change from base Build 17361858204: -0.5%
Covered Lines: 12489
Relevant Lines: 17745

💛 - Coveralls

@cstockton

Copy link
Copy Markdown
Contributor Author

I'll commit my tests and add some additional ones to bring up coverage, wanted to get PR up for eyes on changes.

@cstockton
cstockton merged commit e40e306 into cs/background-template-reloading-p2 Sep 11, 2025
@cstockton
cstockton deleted the cs/background-template-reloading-p3 branch September 11, 2025 22:10
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.

3 participants