Thanks to visit codestin.com
Credit goes to github.com

Skip to content

events: tell CertMagic which events are worth emitting - #8010

Open
u5surf wants to merge 1 commit into
caddyserver:masterfrom
u5surf:events-should-emit
Open

events: tell CertMagic which events are worth emitting#8010
u5surf wants to merge 1 commit into
caddyserver:masterfrom
u5surf:events-should-emit

Conversation

@u5surf

@u5surf u5surf commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #7997 and caddyserver/certmagic#406, and the last piece of #7996.

Draft until CertMagic cuts a release. go.mod currently 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. GetCertificateWithContext emits tls_get_certificate as its first statement, and Go evaluates the argument before emit is entered, so the map and the ClientHello copy were assembled on every handshake regardless.

caddyserver/certmagic#406 added Config.ShouldEmitFunc for exactly this: an embedder answers whether an event is worth emitting, and CertMagic skips building the data when it is not.

Change

App.ShouldEmit becomes exported and is passed alongside OnEvent:

func (app *App) ShouldEmit(eventName string) bool {
	return app.subscriptions[eventName] != nil ||
		app.subscriptions[""] != nil ||
		app.logger.Core().Enabled(zapcore.DebugLevel)
}

The debug term matters. Emit writes 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

CertLookupWithEventsApp is CertMagic's per-handshake certificate lookup, wired the way Caddy wires it. Not a full handshake — no TLS crypto, no I/O.

ns/op B/op allocs/op
master 1032 2480 15
this branch 540 1824 10

For the whole series, starting before #7997: 2045 → 540 ns/op, 5996 → 1824 B/op, 41 → 10 allocs/op.

ShouldEmit is 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.

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.
@francislavoie francislavoie added the optimization 📉 Performance or cost improvements label Sep 11, 2026

@francislavoie francislavoie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@francislavoie francislavoie added this to the v2.11.5 milestone Sep 11, 2026

@steadytao steadytao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cheers. LGTM.

@u5surf

u5surf commented Sep 11, 2026

Copy link
Copy Markdown
Contributor Author

@steadytao Thank you. Can you bump up certmagic? Then I'll updated officially from pseudo-version.

@steadytao

Copy link
Copy Markdown
Member

@mholt probably bump certmagic, then merge this, then do Caddy release.

@u5surf
u5surf marked this pull request as ready for review September 12, 2026 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimization 📉 Performance or cost improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants