Skip to content

feat: background template reloading p2 - mailer template cache & interface refactor - #2150

Merged
cstockton merged 2 commits into
cs/background-template-reloadingfrom
cs/background-template-reloading-p2
Sep 11, 2025
Merged

cstockton merged 2 commits into
cs/background-template-reloadingfrom
cs/background-template-reloading-p2

Conversation

@cstockton

Copy link
Copy Markdown
Contributor

Summary
This PR builds on auth#2148 to deliver phase 2, a robust template cache plus a simplified mail client interface. Templates are now rendered once per type, cached with controlled refresh, and then delivered via clients that only send pre-rendered subject/body. This reduces latency and removes templating logic from SMTP clients while preserving current behavior. It will also pave the way for alternative Mailer implementations.

Template cache + interface refactor

  • Replace TemplateMailer with templatemailer.Mailer that owns all templating and a shared cache. Entries have a TTL and a shorter “recheck interval”; on fetch/parse errors we serve the last good or fall back to built-in defaults.
  • Centralize default subjects/bodies and validate them at init. Typed template keys are: invite, confirmation, recovery, email_change, magic_link, reauthentication.
  • Move headers logic into the mailer; keep $messageType substitution and the legacy “reauthenticate” header name for compatibility.
  • Change mailer.Client.Mail to (ctx, to, subject, body, headers, typ) and update all clients accordingly (SMTP/Noop/Validate/Tasks).
  • Persist a single mailer instance in API; add WithMailer for DI; remove on-demand construction.

- introduce shared template cache
- centralize default subjects/bodies and validate at init
- move templating into templatemailer
- change mailer.Client.Mail signature to (subject, body, headers, typ)
- remove per-client template fetching/func maps
- update existing client packages to match new package flow & interfaces
- api now holds a persistent mailer
- written with intent to preserve all existing behavior
@cstockton
cstockton requested a review from a team as a code owner August 31, 2025 20:15
@coveralls

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 17361858204

Details

  • 237 of 376 (63.03%) changed or added relevant lines in 8 files are covered.
  • 39 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.3%) to 70.92%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/mailer/mailmeclient/mailmeclient.go 0 1 0.0%
internal/mailer/validateclient/validateclient.go 0 2 0.0%
internal/mailer/templatemailer/templatemailer.go 52 58 89.66%
internal/mailer/taskclient/taskclient.go 0 8 0.0%
internal/api/options.go 0 9 0.0%
internal/mailer/templatemailer/template.go 180 293 61.43%
Files with Coverage Reduction New Missed Lines %
internal/api/token_oidc.go 39 14.15%
Totals Coverage Status
Change from base Build 17310237126: 0.3%
Covered Lines: 12489
Relevant Lines: 17610

💛 - Coveralls

)

**Summary**
This PR builds on
[auth#2150](#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.

---------

Co-authored-by: Chris Stockton <[email protected]>
@cstockton
cstockton merged commit 48d98ce into cs/background-template-reloading Sep 11, 2025
@cstockton
cstockton deleted the cs/background-template-reloading-p2 branch September 11, 2025 22:15
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.

4 participants