events: tell CertMagic which events are worth emitting - #8010
Open
u5surf wants to merge 1 commit into
Open
Conversation
CertMagic assembles an event's data before emit() can look at OnEvent, because Go evaluates arguments first, so tls_get_certificate costs a map and a ClientHello copy on every handshake even when nothing will read them. caddyserver/certmagic#406 added Config.ShouldEmitFunc so an embedder can answer that question first. Export App.ShouldEmit and pass it along with OnEvent. The predicate has to account for every observer, not just subscribed handlers: Emit writes a debug line for each event, so an event is observable whenever debug logging is on. Emit now asks the same method it exports, so the two cannot drift. Benchmarks are included; measurements are in the pull request.
Contributor
Author
|
@steadytao Thank you. Can you bump up certmagic? Then I'll updated officially from pseudo-version. |
Member
|
@mholt probably bump certmagic, then merge this, then do Caddy release. |
u5surf
marked this pull request as ready for review
September 12, 2026 00:36
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.
Follow-up to #7997 and caddyserver/certmagic#406, and the last piece of #7996.
Draft until CertMagic cuts a release.
go.modcurrently points at a pseudo-version of the merged commit; it needs to become a tag before this can merge.Why
#7997 made dispatch cheap when nothing is subscribed, but it could not touch the data CertMagic had already built.
GetCertificateWithContextemitstls_get_certificateas its first statement, and Go evaluates the argument beforeemitis entered, so the map and theClientHellocopy were assembled on every handshake regardless.caddyserver/certmagic#406 added
Config.ShouldEmitFuncfor exactly this: an embedder answers whether an event is worth emitting, and CertMagic skips building the data when it is not.Change
App.ShouldEmitbecomes exported and is passed alongsideOnEvent:The debug term matters.
Emitwrites a line for every event, so an event is observable whenever debug logging is on, even with nothing subscribed. A predicate that answered the literal "do I have subscribers?" question would silence that line — the trap @steadytao flagged when reviewing the CertMagic side.Emit's own shortcut now calls this method rather than repeating the condition, so the two cannot drift apart.Benchmarks
CertLookupWithEventsAppis CertMagic's per-handshake certificate lookup, wired the way Caddy wires it. Not a full handshake — no TLS crypto, no I/O.For the whole series, starting before #7997: 2045 → 540 ns/op, 5996 → 1824 B/op, 41 → 10 allocs/op.
ShouldEmitis covered for four states: nothing subscribed, a handler bound by name, a catch-all subscription, and debug logging on with nothing subscribed.Assistance Disclosure
I investigated this with Claude Code (Claude Opus 5). It read the code paths, wrote the patch, the benchmarks and the tests, and ran the test suite. I directed the investigation, reviewed the change, and vetted it for correctness.