Skip to content

swift-resource-pool v0.1.0 πŸš€

Choose a tag to compare

@coenttb coenttb released this 07 Oct 13:19

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