This is a submission for DEV's Summer Bug Smash: Clear the Lineup powered by Sentry.
Project Overview
auth-server is a Go-based authentication service (using the Gin framework) that, among other things, handles sending transactional emails, like email verification and password reset links, to users.
Bug Fix or Performance Improvement
Every time the server sent an email, it read the corresponding HTML template from disk and parsed it from scratch, even though the template content never changes at runtime. This meant unnecessary disk I/O and template-parsing overhead on every single email send, which is wasteful under any real load.
Code
PR: https://github.com/roshankumar0036singh/auth-server/pull/121
Closes Issue: #86
My Improvements
I added a templates cache field to the EmailService struct and moved template parsing to a loadTemplates() call that runs once at startup via NewEmailService. SendEmail now just reads from the in-memory cache instead of hitting disk on every request.
During review, a few important issues came up that pushed the fix further than my first pass:
-
Graceful startup: originally a missing template would crash the server via
log.Fatalf. I changed this to log a warning and continue, withErrTemplateNotFoundreturned only when that specific template is actually requested. -
Thread safety: since the cache is shared across concurrent goroutines handling requests, I had to make sure
template.Templateexecution was safe under concurrent access rather than assuming a single cached pointer was automatically safe to reuse everywhere. -
A tricky merge conflict: a later commit accidentally discarded the template value (
t, okchanged to_, ok) while a downstream line still calledt.Execute(...), which broke the build. I traced it back through the diff and restored the correct binding.
The result: no repeated disk reads, safer concurrent access, and the server no longer crashes on a missing template file. It degrades gracefully instead.
Top comments (0)