Skip to main content

Overview

The responsesize middleware tracks response sizes for monitoring, logging, and metrics collection. Use it when you need:
  • Monitor response sizes
  • Collect bandwidth metrics
  • Detect response size anomalies

Installation

Quick Start

Configuration

Options

Examples

Track in Context

With Callback

Log Large Responses

API Reference

Functions

Technical Details

Implementation Architecture

The middleware uses a wrapping pattern to intercept and track response writes:
  1. Context Storage: Response size information is stored in the request context using a custom contextKey type for type-safe retrieval.
  2. Writer Wrapping: The middleware wraps the original http.ResponseWriter with a trackingWriter that intercepts all Write() calls.
  3. Atomic Tracking: Uses sync/atomic operations to safely track bytes written, ensuring thread-safe counting even with concurrent writes.
  4. Callback Mechanism: After the response is fully written, an optional callback (OnSize) is invoked with the total byte count.

Key Components

  • Info struct: Holds the bytesWritten counter with an atomic accessor method BytesWritten().
  • trackingWriter: A wrapper around http.ResponseWriter that increments the byte counter on each write.
  • contextKey: An empty struct used as a unique key for storing size info in the context.

Flow

  1. Create Info instance and store in context
  2. Wrap response writer with trackingWriter
  3. Execute next handler in chain
  4. Invoke OnSize callback with final byte count
  5. Restore original writer

Best Practices

  • Track alongside request sizes
  • Set alerts for unusually large responses
  • Use for bandwidth billing
  • Monitor compression effectiveness

Testing

The middleware includes comprehensive test coverage: