目录
目录README.md

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

关于
40.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号