Skip to content

Commit

Permalink
Log the maximum allowed number of attempts.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumoboy007 committed Jun 5, 2024
1 parent c3481fd commit 416667f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Sources/Retry/Logger+RetryMetadataKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import Logging
extension Logger {
/// The metadata keys used by the retry implementation.
public enum RetryMetadataKey: String {
/// The maximum number of attempts that are allowed.
///
/// The key will be absent if there is no configured maximum.
case maxAttempts = "retry.configuration.max_attempts"

/// The one-based attempt number.
case attemptNumber = "retry.attempt"

Expand Down
16 changes: 14 additions & 2 deletions Sources/Retry/Retry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ public func retry<ClockType, ReturnType>(

let recoverFromFailure = configuration.recoverFromFailure

if let maxAttempts {
logger?[metadataKey: .maxAttempts] = "\(maxAttempts)"
}
#if canImport(OSLog)
let maxAttemptsSentenceFragment: String
if let maxAttempts {
maxAttemptsSentenceFragment = " of \(maxAttempts)"
} else {
maxAttemptsSentenceFragment = ""
}
#endif

var attempt = 1
while true {
var latestError: any Error
Expand Down Expand Up @@ -360,7 +372,7 @@ public func retry<ClockType, ReturnType>(
])
#if canImport(OSLog)
appleLogger?.debug("""
Attempt \(attempt, privacy: .public) failed with error of type `\(type(of: latestError), privacy: .public)`: `\(latestError)`. \
Attempt \(attempt, privacy: .public)\(maxAttemptsSentenceFragment, privacy: .public) failed with error of type `\(type(of: latestError), privacy: .public)`: `\(latestError)`. \
Will wait \(String(describing: delay), privacy: .public) before retrying.
""")
#endif
Expand Down Expand Up @@ -405,7 +417,7 @@ public func retry<ClockType, ReturnType>(
])
#if canImport(OSLog)
appleLogger?.debug("""
Attempt \(attempt, privacy: .public) failed with error of type `\(type(of: latestError), privacy: .public)`: `\(latestError)`. \
Attempt \(attempt, privacy: .public)\(maxAttemptsSentenceFragment, privacy: .public) failed with error of type `\(type(of: latestError), privacy: .public)`: `\(latestError)`. \
The `recoverFromFailure` closure requested a minimum delay of \(String(describing: minDelay)) before retrying. \
Will wait \(String(describing: delay), privacy: .public) before retrying.
""")
Expand Down

0 comments on commit 416667f

Please sign in to comment.