ITADN
swift-foundations/swift-resource-pool

版本发布 2

0.1.4
? · 2025-12-19

## What's Changed * Lowered: Platform requirements to macOS 13+, iOS 16+, tvOS 16+, watchOS 9+ (https://github.com/coenttb/swift-resource-pool/pull/7) - thanks @loparker-CLGX! * Added: iOS simulator tests to CI * Changed: Restructured Package@swift files (Package.swift now Swift 6.0, Package@swift-5.9.swift for older toolchains) * Fixed: Skip timing-sensitive tests on iOS simulator **Full Changelog**: https://github.com/coenttb/swift-resource-pool/compare/0.1.3...0.1.4

swift-resource-pool v0.1.0 🚀0.1.0
? · 2025-10-07

# 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`: ```swift dependencies: [ .package(url: "https://github.com/coenttb/swift-resource-pool", from: "0.1.0") ] ``` --- ## 🚀 Quick Start ```swift 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](https://github.com/coenttb/swift-resource-pool#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