A high-concurrency microservice built with Go and gRPC to handle email delivery and OTP management.
- The Problem: Standard
net/smtpis slow because it creates a new connection (TCP handshake + TLS + Auth) for every single email sent. - Our Solution: Implements a Worker Pool with Persistent Connections. Workers pre-dial and stay authenticated, allowing emails to be dispatched instantly with zero handshake latency.
- Zero-Latency Sending: Persistent SMTP connections eliminate handshake overhead.
- Concurrency: Configurable worker pool (default: 5) handles multiple clients simultaneously without blocking.
- Thread-Safe OTP: In-memory storage protected by
sync.RWMutexwith atomic validation logic to prevent race conditions. - Auto-Cleanup: Background "Janitor" worker removes expired OTPs to prevent memory leaks.
- Graceful Shutdown: Completes active jobs before closing connections on
SIGINTorSIGTERM.
PORT=40700
WORKERS=5 # Concurrent SMTP connections
OTP_CLEANUP_MINUTES=5 # Expiry check interval
# SMTP Settings (e.g., Gmail)
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=465
EMAIL=your_email@gmail.com
APP_PASSWORD=xxxx-xxxx-xxxx-xxxx- Setup
go mod tidy
make generate # Re-generates Protobuf code- Run
go run main.go| Method | Inputs | Description |
|---|---|---|
SendCode |
email |
Generates a 6-digit code, stores it in memory, and dispatches an email via the worker pool. |
ValidateCode |
email, otp |
Thread-safe validation. Automatically deletes the code upon success to prevent replay attacks. |
| Method | Inputs | Description |
|---|---|---|
SendMail |
to, subject, body |
Asynchronously queues a raw email to be processed by the next available worker. |