gpt4 book ai didi

iOS - 重试机制

转载 作者:搜寻专家 更新时间:2023-11-01 06:58:00 24 4
gpt4 key购买 nike

所以我正在尝试为我的网络调用(第一层)构建一个重试函数。这是函数:

func retry<T>(_ attempts: Int, task: @escaping (_ success: @escaping (T) -> Void, _ failure: @escaping (String) -> Void) -> Void, success: @escaping (T) -> Void, failure: @escaping (String) -> Void) {
task({ (obj) in
success(obj)
}) { (error) in
print("Error retry left \(attempts)")
if attempts > 1 {
self.retry(attempts - 1, task: task, success: success, failure: failure)
} else {
failure(error)
}
}
}

实现是这样的:

func refreshSession(success: @escaping () -> Void, failure: @escaping (String) -> Void) {
cameraProtocols?.refreshSession( success: {
print("calling serverping")
self.timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true, block: { (timer) in
self.keepAlive(session: CameraManager.session, success: {
print("serverping succsess")
success()
}, failure: { (error) in
print(error)
failure(error)
})
})
}, failure: { (error) in
print(error)
failure(error)
})
}

func keepAlive(session: String, success: @escaping () -> Void, failure: @escaping (String) -> Void) {
cameraProtocols?.keepAlive(session: CameraManager.session, success: {
print("server ping done!")
NotificationCenter.default.post(name: Notification.Name(rawValue: "pingSuccess"), object: nil)
success()
}, failure: { (error) in
print(error)
self.retry(3, task: { (success, failure) in
self.refreshSession(success: success, failure: failure)
}, success: { (success) in
print("refresh succsed from retry")
success
}, failure: { (e) in
print("refresh failed from retry: \(e)")
//TODO - handle error logic when to procced with the failure closure
failure(error)
})
NotificationCenter.default.post(name: Notification.Name(rawValue: "faildPing"), object: nil)
})
}

当我故意使 keepalive 调用失败时,重试不会按预期工作并且永远不会停止。

对新功能或修复方法有什么建议吗?

最佳答案

我认为 CircuitBreaker 模式 是您所需要的

断路器背后的基本思想非常简单。您将 protected 函数调用包装在断路器对象中,该对象会监视故障。一旦故障达到某个阈值,断路器就会跳闸,所有对断路器的进一步调用都会返回错误,而根本不会进行 protected 调用

参见 this

关于iOS - 重试机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52353684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com