Repository navigation
feat: background template reloading p2 - mailer template cache & interface refactor - #2150
Merged
cstockton merged 2 commits intoSep 11, 2025
Conversation
- 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
Pull Request Test Coverage Report for Build 17361858204Details
💛 - Coveralls |
hf
approved these changes
Sep 1, 2025
cemalkilic
reviewed
Sep 1, 2025
) **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]>
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
TemplateMailerwithtemplatemailer.Mailerthat 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.invite,confirmation, recovery,email_change,magic_link,reauthentication.$messageTypesubstitution and the legacy “reauthenticate” header name for compatibility.mailer.Client.Mailto(ctx, to, subject, body, headers, typ)and update all clients accordingly (SMTP/Noop/Validate/Tasks).API; addWithMailerfor DI; remove on-demand construction.