swift-resource-pool v0.1.0 π
swift-resource-pool v0.1.0 π
Production-ready actor-based resource pool for Swift
A high-performance, thread-safe resource pooling solution that eliminates the thundering herd problem through direct resource handoff, ensures fairness with FIFO ordering, and provides comprehensive metrics for production monitoring.
β¨ Key Features
π― Zero Thundering Herd
- Direct handoff: Resources go to exactly ONE waiting task (no broadcast storms)
- O(1) wakeup: Efficient queue management with 90-95% handoff rate
- Proven scalability: Handles 200+ concurrent waiters without degradation
βοΈ Fairness & Predictability
- FIFO queue: Prevents starvation, ensures fair ordering
- LIFO resource selection: Optimizes cache locality
- Consistent performance: P99/Avg latency ratio of only 1.2x
π‘οΈ Production Ready
- Cancellation-safe: Guaranteed cleanup on task cancellation
- Actor-isolated: Thread-safe without locks
- Zero leaks: All resources and continuations properly managed
- Defensive programming: Parameter validation, overflow protection
β‘ High Performance
- Up to 2,395 ops/sec throughput (capacity-dependent)
- Near-linear scaling: 3.28x improvement for 3.3x load increase
- Efficient warmup: Non-blocking background pre-creation
- Automatic cleanup: Validation and reset between uses
π Comprehensive Observability
- Real-time statistics (utilization, queue depth, backpressure)
- Production metrics (acquisitions, timeouts, handoffs, wait times)
- 45 tests with 100% pass rate
π¦ Installation
Add to your Package.swift:
dependencies: [
.package(url: "https://github.com/coenttb/swift-resource-pool", from: "0.1.0")
]π Quick Start
import ResourcePool
// Define your poolable resource
actor DatabaseConnection: PoolableResource {
struct Config: Sendable {
let host: String
let port: Int
}
static func create(config: Config) async throws -> DatabaseConnection {
// Create connection
}
func validate() async -> Bool {
// Check if connection is alive
}
func reset() async throws {
// Reset connection state
}
}
// Create pool with 10 connections
let pool = try await ResourcePool<DatabaseConnection>(
capacity: 10,
resourceConfig: .init(host: "localhost", port: 5432),
warmup: true
)
// Use resources safely with automatic cleanup
let results = try await pool.withResource { connection in
try await connection.query("SELECT * FROM users")
}π Performance (Verified)
| Metric | Value |
|---|---|
| Throughput (cap 20, 100 tasks) | 2,395 ops/sec |
| Scalability (30β100 waiters) | 3.28x improvement |
| Handoff efficiency | 90-95% |
| P99 latency (under contention) | 171ms |
| P99/Avg ratio | 1.2x |
| Stress test (500 concurrent ops) | 0 timeouts |
β Test Coverage
- 45 tests across 4 test suites
- 100% pass rate in production
- Basic functionality (7 tests)
- Concurrency & fairness (13 tests)
- Lifecycle & cleanup (12 tests)
- Performance benchmarks (13 tests)
π§ Requirements
- Swift 5.9+ (Swift 6.0 language mode for strict concurrency)
- macOS 14+, iOS 17+, tvOS 17+, watchOS 10+, Linux
- Zero external dependencies
π Documentation
See the README for:
- Detailed usage examples (Database, HTTP Client, WKWebView)
- API reference
- Advanced patterns (global actors, monitoring, graceful shutdown)
- Real-world performance benchmarks
π Acknowledgments
Built with Swift's modern concurrency features: actors, async/await, and structured concurrency.
π License
Apache 2.0 with Runtime Library Exception
Ready for production use! π
Report issues: https://github.com/coenttb/swift-resource-pool/issues
Discussions: https://github.com/coenttb/swift-resource-pool/discussions