SwiftPolling is a lightweight, Swift 6-compatible polling utility that allows for flexible and customizable polling strategies. It supports multiple interval strategies such as linear and exponential backoff, making it ideal for scenarios where periodic execution is required.
- Actor-based implementation for thread safety
- Multiple polling strategies
- Linear intervals
- Exponential backoff
- Exponential backoff with a max interval
- Custom polling strategies by conforming to
PollingStrategy
- Async/Await support using
AsyncThrowingStream
- Graceful cancellation support
Add the following dependency to your Package.swift
:
.package(url: "https://github.com/yosi99/SwiftPolling.git", from: "1.0.0")
Then add it as a dependency in your target:
.target(name: "YourProject", dependencies: ["SwiftPolling"])
The PollingService
uses a PollingStrategy
to determine intervals between operations. You can choose from different built-in strategies.
import SwiftPolling
let pollingService = PollingService(strategy: LinearPollingStrategy(maxRuns: 5, durationBetweenRuns: 2))
This will execute the polling operation 5 times with a 2-second interval.
let pollingService = PollingService(strategy: ExponentialPollingStrategy(maxRuns: 5, durationBetweenRuns: 1))
This will increase the interval exponentially (e.g., 1s, 2s, 4s, etc.).
let pollingService = PollingService(strategy: ExponentialWithMaxPollingStrategy(maxRuns: 5, durationBetweenRuns: 1, maxInterval: 8))
This ensures that the interval does not exceed the specified maxInterval
.
To start polling, use the stream()
method and handle each iteration:
Task {
for try await iteration in pollingService.stream() {
print("Polling iteration: \(iteration)")
}
}
You can cancel polling anytime by calling:
pollingService.finish()
You can create your own custom polling strategy by conforming to the PollingStrategy
protocol and implementing custom logic:
struct CustomPollingStrategy: PollingStrategy {
let maxRuns: Int
let durationBetweenRuns: TimeInterval
func make(currentIteration: Int) -> PollingStrategy {
return CustomPollingStrategy(maxRuns: maxRuns, durationBetweenRuns: durationBetweenRuns + Double(currentIteration))
}
}
This allows you to define polling intervals that increase or change based on custom logic.
Contributions are welcome! Feel free to open issues or submit pull requests.
This project is licensed under the MIT License. See the LICENSE
file for more details.