Thanks, Konrad!
Hystrix looks really useful for building resilient distributed systems. Thanks for sharing! The library looks quite comprehensive, featuring a convenient wrapper for dependencies that enforces resource quotas, publishes metrics, adds a circuit breaker, etc.
My intention was for swift-retry to be a modular package focused solely on retries while being flexible enough to be used as a building block by higher-level libraries like Hystrix.
For example, the RetryConfiguration type encapsulates all of the retry behavior, so it could be exposed in the public configuration API of a hypothetical Hystrix library. The hypothetical Hystrix library could override the recoverFromFailure closure to, for example, customize the handling of a circuit breaker error:
var configuration = configuration
configuration.recoverFromFailure = { error in
switch error {
case let error as CircuitBreakerIsOpen:
// Do not bother retrying until after the circuit breaker
// transitions from the open state to the half-open state.
return .retryAfter(error.nextHalfOpenInstant)
default:
return configuration.recoverFromFailure(error)
}
}
Regarding metrics, I’m not sure swift-retry is the right place for them. First, the metrics have per-application, per-operation namespaces and dimensions that the caller would have to provide. Second, different applications might have different requirements (e.g. a metric per error case vs. a single metric for all errors).
Given the variability in requirements, swift-retry wouldn’t be able to provide automatic metrics functionality without also providing additional API to configure that functionality. swift-retry should already be flexible enough for a caller to add their own metrics though:
let startInstant = clock.now
defer {
let totalDuration = clock.now - startInstant
timer.record(totalDuration)
}
try await retry {
do {
try await doSomething()
successCounter.increment()
} catch {
failureCounter.increment()
throw error
}
}
Hopefully, the developer of a hypothetical Hystrix library will find it easy to build on top of swift-retry. I’m happy to try to make swift-retry more flexible as needed!
My swift-http-error-handling package, which is built on top of swift-retry, is also intended to be modular. Check it out if you haven’t already! 