Skip to content

Conversation

@yudhasubki
Copy link
Owner

@yudhasubki yudhasubki commented Dec 14, 2025

Summary

This PR improves connection pool safety, adds dial timeout support, and modernizes context cancellation handling.

Changes

🚀 New Features

  • Dial Timeout: Added WithDialTimeout(d time.Duration) option to prevent indefinite blocking when dialing unresponsive servers
    • New ErrDialTimeout error when dial exceeds configured timeout
    • Proper cleanup of late-completing dial operations

🛡️ Safety Improvements

  • Added nil-check protection in Read() and Write()(cci:1://file:///Users/yudhapratamaputra/Project/yudhasubki/netpool/pool_connection.go:97:0-113:1) to prevent race condition panics
  • Override SetDeadline, SetReadDeadline, SetWriteDeadline, LocalAddr, RemoteAddr methods with proper closed-connection checks
  • Auto-reset deadline in Close() before returning connection to pool (prevents deadline leakage to next user)
  • Fixed MarkUnusable() to properly return object to sync.Pool

netpool.go:

  • Replaced goroutine+channel pattern with context.AfterFunc (Go 1.21+) for cleaner context cancellation handling

Usage Example

pool, err := netpool.New(dialFn,
    netpool.WithMinPool(5),
    netpool.WithMaxPool(50),
    netpool.WithDialTimeout(5*time.Second),
)

@yudhasubki yudhasubki merged commit 5e2e0dc into main Dec 14, 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.

1 participant