Skip to content

Conversation

@yudhasubki
Copy link
Owner

Refactor: Netpool v2 - Zero Allocation Lock-Free Pool

Summary

This PR completely rewrites netpool to be the fastest Go connection pool available.
It replaces the previous mutex/map-based implementation with a lock-free design using channels and atomic operations.

We also introduced a Pass-by-Value optimization for connection wrappers, achieving Zero Allocations and 118ns/op latency (Standard) or 42ns/op (Basic).

Benchmark Results

Library ns/op Memory Allocations
netpool (Basic) 42 ns 0 B 0 allocs
netpool (Standard) 118 ns 0 B 0 allocs
fatih/pool 124 ns 64 B 1 alloc
silenceper/pool 303 ns 48 B 1 alloc

Improvements:

  • Speed: Standard is ~5x faster, Basic is ~15x faster than previous version.
  • Memory: 0 B/op (was ~48 B/op)
  • Features: Preserved IdleTimeout and added HealthCheck without performance cost.

Breaking Changes

This release introduces significant API changes to achieve maximum performance.

1. New Config Structure

Functional options (WithMinPool, etc.) are replaced with a simple Config struct.

2. Manual Put Required (No Auto-Return)

Connections returned by Get() are no longer wrapped with auto-return logic.
You MUST call pool.Put(conn) explicitly.

3. Removed Features

  • DialHooks: Removed to simplify the hot path.
  • MaxIdleTime Option: Now part of Config struct.

@yudhasubki yudhasubki merged commit 243a123 into main Dec 15, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants