RetryStrategy.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // RetryStrategy.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2020/05/04.
  6. //
  7. // Copyright (c) 2020 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. public class RetryContext {
  28. public let source: Source
  29. public let error: KingfisherError
  30. public var retriedCount: Int
  31. public internal(set) var userInfo: Any? = nil
  32. init(source: Source, error: KingfisherError) {
  33. self.source = source
  34. self.error = error
  35. self.retriedCount = 0
  36. }
  37. @discardableResult
  38. func increaseRetryCount() -> RetryContext {
  39. retriedCount += 1
  40. return self
  41. }
  42. }
  43. public enum RetryDecision {
  44. case retry(userInfo: Any?)
  45. case stop
  46. }
  47. public protocol RetryStrategy {
  48. func retry(context: RetryContext, retryHandler: @escaping (RetryDecision) -> Void)
  49. }
  50. public struct DelayRetryStrategy: RetryStrategy {
  51. public enum Interval {
  52. case seconds(TimeInterval)
  53. case accumulated(TimeInterval)
  54. case custom(block: (_ retriedCount: Int) -> TimeInterval)
  55. func timeInterval(for retriedCount: Int) -> TimeInterval {
  56. let retryAfter: TimeInterval
  57. switch self {
  58. case .seconds(let interval):
  59. retryAfter = interval
  60. case .accumulated(let interval):
  61. retryAfter = Double(retriedCount + 1) * interval
  62. case .custom(let block):
  63. retryAfter = block(retriedCount)
  64. }
  65. return retryAfter
  66. }
  67. }
  68. public let maxRetryCount: Int
  69. public let retryInterval: Interval
  70. public init(maxRetryCount: Int, retryInterval: Interval = .seconds(3)) {
  71. self.maxRetryCount = maxRetryCount
  72. self.retryInterval = retryInterval
  73. }
  74. public func retry(context: RetryContext, retryHandler: @escaping (RetryDecision) -> Void) {
  75. // Retry count exceeded.
  76. guard context.retriedCount < maxRetryCount else {
  77. retryHandler(.stop)
  78. return
  79. }
  80. // User cancel the task. No retry.
  81. guard !context.error.isTaskCancelled else {
  82. retryHandler(.stop)
  83. return
  84. }
  85. // Only retry for a response error.
  86. guard case KingfisherError.responseError = context.error else {
  87. retryHandler(.stop)
  88. return
  89. }
  90. let interval = retryInterval.timeInterval(for: context.retriedCount)
  91. if interval == 0 {
  92. retryHandler(.retry(userInfo: nil))
  93. } else {
  94. DispatchQueue.main.asyncAfter(deadline: .now() + interval) {
  95. retryHandler(.retry(userInfo: nil))
  96. }
  97. }
  98. }
  99. }