https://github.com/swiftuiux/retry-policy-service
Retry policies for network requests in swift DispatchTimeInterval to Duration swift retry swift Retry strategies library Retry policies for network requests Retry strategies for API calls Configurable retry strategies for REST Exponential backoff algorithm
https://github.com/swiftuiux/retry-policy-service
concurrency duration ios retry-intervals retry-pattern retry-policy retry-strategies retry-swift retrying swift timeinterval
Last synced: 7 months ago
JSON representation
Retry policies for network requests in swift DispatchTimeInterval to Duration swift retry swift Retry strategies library Retry policies for network requests Retry strategies for API calls Configurable retry strategies for REST Exponential backoff algorithm
- Host: GitHub
- URL: https://github.com/swiftuiux/retry-policy-service
- Owner: swiftuiux
- License: mit
- Created: 2023-03-07T07:36:13.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-14T15:28:41.000Z (9 months ago)
- Last Synced: 2024-11-28T18:41:36.532Z (7 months ago)
- Topics: concurrency, duration, ios, retry-intervals, retry-pattern, retry-policy, retry-strategies, retry-swift, retrying, swift, timeinterval
- Language: Swift
- Homepage:
- Size: 22.5 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Retry service provides policy for how often some operation should happen with the timeout limit and delay between events
The service creates sequence of the delays (nanoseconds) according to chosen strategy
[](https://swiftpackageindex.com/swiftuiux/retry-policy-service)
There are two strategies
| type | description |
| --- | --- |
| constant | The strategy implements constant backoff |
| exponential [default] | Exponential backoff is a strategy in which you increase the delays between retries |```swift
/// Constant delay between retries
case constant(
retry : UInt = 5,
duration: DispatchTimeInterval,
timeout: DispatchTimeInterval
)
/// Exponential backoff is a strategy in which you increase the delays between retries
case exponential(
retry : UInt = 3,
multiplier: Double = 2.0, // power exponent
duration: DispatchTimeInterval,
timeout: DispatchTimeInterval
)```
## SwiftUI example
[example for retry service](https://github.com/swiftuiux/retry-policy-service-example)
## Packages using the package
[Async http client](https://github.com/swiftuiux/async-http-client)
## How to use
```swift
final class ViewModel : ObservableObject{
func constant() async {
let policy = RetryService(strategy: .constant())
for delay in policy{
try? await Task.sleep(nanoseconds: delay)
// do something
}
}
func exponential() async {
let policy = RetryService(
strategy: .exponential(
retry: 5,
multiplier: 2,
duration: .seconds(1),
timeout: .seconds(5)
)
)
for delay in policy{
try? await Task.sleep(nanoseconds: delay)
// do something
}
}
}struct ContentView: View {
@StateObject var model = ViewModel()
var body: some View {
VStack {
Button("constatnt") { Task { await model.constant() } }
Button("exponential") { Task { await model.exponential() } }
}
.padding()
.task {
await model.exponential()
}
}
}
```## TODO:
Exponential backoff with jitter. Jitter adds some amount of randomness to the backoff to spread the retries around in time.
For more information [Exponential Backoff And Jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/)