Skip to main content

Overview

The bodyclose middleware ensures request bodies are properly closed after processing, preventing resource leaks. Use it when you need:
  • Prevent connection leaks
  • Ensure proper resource cleanup
  • Handle abandoned requests

Installation

Quick Start

Examples

Basic Usage

With Drain

API Reference

Functions

Options

Technical Details

The bodyclose middleware operates by wrapping the request handler with a deferred cleanup function that ensures proper resource management:

Implementation Strategy

  1. Nil Body Check: Immediately passes control to the next handler if the request body is nil, avoiding unnecessary overhead
  2. Deferred Cleanup: Uses Go’s defer statement to guarantee body cleanup regardless of handler outcome (success, error, or panic)
  3. Optional Draining: When DrainBody is enabled, reads and discards up to MaxDrain bytes before closing to enable HTTP keep-alive connection reuse
  4. Connection Reuse: By draining the body, the middleware signals to the HTTP client that the connection can be safely reused in connection pools

Resource Management

  • Default MaxDrain: 8KB (8192 bytes) to balance connection reuse benefits with performance
  • Drain Mechanism: Uses io.CopyN to limit the amount of data drained, preventing excessive memory consumption on large bodies
  • Error Handling: Silently ignores drain and close errors to prevent interference with the main request flow

Performance Characteristics

  • Minimal Overhead: Only adds a single function call and defer statement per request
  • Memory Efficient: Draining is capped by MaxDrain to prevent memory exhaustion
  • No Blocking: Draining respects the MaxDrain limit and doesn’t attempt to fully read unbounded bodies

Why It Matters

Without proper body closing:
  • Connections may not be reused
  • Memory leaks can occur
  • Connection pool exhaustion

Best Practices

  • Place early in middleware chain
  • Use with connection pooling
  • Consider draining for HTTP keep-alive

Testing

The bodyclose middleware includes comprehensive test coverage to ensure proper functionality:

Test Coverage

The test suite uses a custom trackingBody type that monitors:
  • Whether the body was closed (closed field)
  • Whether the body was fully drained (drained field)
This ensures that the middleware behaves correctly in various scenarios including unread bodies, partial reads, and different configuration options.