Skip to content

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

License

Notifications You must be signed in to change notification settings

igor11191708/retry-policy-service

Repository files navigation

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

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
        /// 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 
        )

Packages using the package

Async http client

How to use

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()
        }
        
    }
}

SwiftUI example for the package

example for retry service

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published