Middleware
Functions that wrap handlers with additional logic like logging, retries, or timeout management.
Middleware is a function that wraps a handler with additional logic, running code before or after (or instead of) the handler itself. The concept is the same in HTTP servers and message processing.
This wrapping pattern lets you add cross-cutting concerns without modifying your business logic. Logging, metrics, distributed tracing, retries, timeouts, panic recovery, correlation ID propagation: all of these fit naturally as middleware.
You can stack multiple middleware on a router. They execute in the order you add them, forming a chain around the handler. Writing custom middleware is straightforward.
patterns messaging
References
- Introducing Watermill - Go event-driven applications library — Shows how Watermill's middleware pattern handles metrics, distributed tracing, poison queues, retrying, and throttling. Defines the HandlerMiddleware type and shows built-in middlewares like InstantAck.
- Increasing Cohesion in Go with Generic Decorators — Explores the decorator pattern in Go, which is the same wrapping approach middleware uses. Describes decorators as application-level middlewares for separating cross-cutting concerns.
- Building a serverless application with Go, Google Cloud Run and Firebase — Demonstrates setting up HTTP middlewares with chi router, including RequestID, RealIP, Recoverer, CORS, and auth. Shows how standard library-compatible middlewares provide flexibility in building custom HTTP servers.
- The Go libraries that never failed us: 22 libraries you need to know — Includes a dedicated section on HTTP middlewares for CORS, CSRF, error handling, logging, and authorization. Compares middleware support in Echo and chi routers and links to the Awesome Go middleware list.
- You should not build your own authentication — Shows how to build a Firebase HTTP authentication middleware that extracts and verifies tokens from HTTP headers and adds user context to the request.
- Durable Background Execution with Go and SQLite — Uses handler middleware for idempotency testing with a duplicator middleware. Demonstrates chaos and context-pollution middlewares paired with retry middleware to prove atomicity in event handlers.
- Watermill 1.4 Released (Event-Driven Go Library) — Introduces a delayed requeue component using router middleware. Combines the Poison middleware, DelayOnError middleware, and PostgreSQL queue adapter behind a simple API.
- Watermill 1.3 released, an open-source event-driven Go library — Introduces the CircuitBreaker middleware implementing the circuit breaker pattern with gobreaker. Helps avoid overloading downstream services by failing fast when handlers fail repeatedly.
- Golang CQRS, Metrics and AMQP - Watermill v0.3.0 released — Describes the metrics component built with handler middlewares and pub/sub decorators. Includes improved stack traces for middleware.Recoverer.
- Live website updates with Go, SSE, and htmx — Shows adding the Recoverer middleware to a Watermill router so panics in handlers don't crash the server, similar to HTTP router middleware patterns.
- Event-Driven Architecture: The Hard Parts — Discusses using a duplicator middleware to process every message twice for idempotency testing. Mentions enabling this middleware in production for financial domains to verify handlers tolerate repeated processing.
- When you shouldn't use frameworks in Go — Discusses middleware support as a key feature when choosing HTTP libraries. Emphasizes wanting a nice API with middleware support while keeping the HTTP package replaceable.
- Watermill: from a hobby project to 8k stars on GitHub — Mentions middleware support as one of the key features when discussing what to look for in an HTTP package, alongside a clean API.
- Watermill Middleware Documentation