CircuitBreaker
public class CircuitBreaker<A, B>
CircuitBreaker enables you to use Circuit Breaker logic in your Swift applications.
- A: Parameter types used in the arguments for the command closure.
- B: Parameter type used as the second argument for the fallback closure.
Usage Example:
The example below shows how to create a CircuitBreaker
instance for each context
function (e.g. endpoint) you wish to circuit break. You need to give the CircuitBreaker instance a name,
create a context function for the endpoint you intend to circuit break (myContextFunction
in the example
below) and define a fallback function (myFallback
in the example below) to call if there are timeouts
or user defined failures occur.
let breaker = CircuitBreaker(name: "Circuit1", command: myContextFunction, fallback: myFallback)
For a more complete example see the CircuitBreaker README.
-
The context function that runs using the given arguments of generic type A.
Declaration
Swift
public typealias AnyContextFunction<A> = (Invocation<A, B>) -> Void
-
The fallback function that runs when the invocation fails.
Declaration
Swift
public typealias AnyFallback<B> = (BreakerError, B) -> Void
-
Name of the CircuitBreaker instance.
Declaration
Swift
public private(set) var name: String
-
Name of the CircuitBreaker group.
Declaration
Swift
public private(set) var group: String?
-
Execution timeout for the command context, that is, the time in milliseconds that your function must complete within before the invocation is considered a failure. Default value is 1000 milliseconds.
Declaration
Swift
public let timeout: Int
-
Timeout to reset the circuit, that is, the time in milliseconds to wait before resetting the circuit to half open state. Default value is 6000 milliseconds.
Declaration
Swift
public let resetTimeout: Int
-
Number of failures allowed within
rollingWindow
before setting the circuit state to open. Default value is 5.Declaration
Swift
public let maxFailures: Int
-
Time window in milliseconds within which the maximum number of failures must occur to trip the circuit. For instance, say
maxFailures
is 5 androllingWindow
is 10000 milliseconds. In such a case, for the circuit to trip, 5 invocation failures must occur in a time window of 10 seconds, even if these failures are not consecutive. Default value is 10000 milliseconds.Declaration
Swift
public let rollingWindow: Int
-
Instance of Circuit Breaker statistics.
Declaration
Swift
public let breakerStats = Stats()
-
The Circuit Breaker’s current state.
Declaration
Swift
public private(set) var breakerState: State
-
Initializes a CircuitBreaker instance with an asyncronous context command.
Declaration
Swift
public init(name: String, group: String? = nil, timeout: Int = 1000, resetTimeout: Int = 60000, maxFailures: Int = 5, rollingWindow: Int = 10000, bulkhead: Int = 0, command: @escaping AnyContextFunction<A>, fallback: @escaping AnyFallback<B>)
Parameters
name
Name of the CircuitBreaker instance.
group
Name of the CircuitBreaker group (optional).
timeout
Execution timeout for command context. That is, the time in milliseconds that your function must complete within before the invocation is considered a failure. Default is set to 1000 milliseconds.
resetTimeout
Time in milliseconds to wait before resetting the circuit to half open state. Default is set to 60000 milliseconds.
maxFailures
Maximum number of failures allowed within
rollingWindow
before opening the circuit. Default is set to 5.rollingWindow
Time window in milliseconds within which the maximum number of failures must occur to trip the circuit. For instance, say
maxFailures
is 5 androllingWindow
is 10000 milliseconds. In such a case, for the circuit to trip, 5 invocation failures must occur in a time window of 10 seconds, even if these failures are not consecutive. Default is set to 10000 milliseconds.bulkhead
Number representing the limit of concurrent requests running at one time. Default is 0, which is equivalent to not using the bulk heading feature.
command
Contextual function to circuit break, which allows user defined failures (the context provides an indirect reference to the corresponding circuit breaker instance).
fallback
Function user specifies to signal timeout or fastFail completion. Required format:
(BreakerError, (fallbackArg1, fallbackArg2,...)) -> Void
-
Runs the circuit using the provided arguments.
Usage Example:
The example below shows how to create and then run a circuit.
let breaker = CircuitBreaker(name: "Circuit1", command: myFunction, fallback: myFallback) breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: "Something went wrong.")
Declaration
Swift
public func run(commandArgs: A, fallbackArgs: B)
Parameters
commandArgs
Arguments of type
A
for the circuit command.fallbackArgs
Arguments of type
B
for the circuit fallback. -
Method to print current statistics.
Usage Example:
The example below shows how to log a snapshot of the statistics for a given CircuitBreaker instance.
let breaker = CircuitBreaker(name: "Circuit1", command: myFunction, fallback: myFallback) breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: "Something went wrong.") breaker.logSnapshot()
Declaration
Swift
public func logSnapshot()
-
Method to force the circuit open.
Declaration
Swift
public func forceOpen()
-
Method to force the circuit closed.
Declaration
Swift
public func forceClosed()
-
Method to force the circuit half open.
Declaration
Swift
public func forceHalfOpen()
-
Method to create a link to a StatsMonitor instance.
Usage Example:
Given a monitor class called
exampleMonitor
which implementsStatsMonitor
you can link this monitor to CircuitBreaker as shown below. During the initialization of the CircuitBreaker instances (circuit1 and circuit2) the linked monitor is notified of their instantiation thus allowing it to begin tracking the statistics for both instances.let monitor1 = exampleMonitor() CircuitBreaker<Any, Any>.addMonitor(monitor: monitor1) let circuit1 = CircuitBreaker(name: "Circuit1", command: myContextFunction, fallback: myFallback) let circuit2 = CircuitBreaker(name: "Circuit2", command: myContextFunction, fallback: myFallback)
Declaration
Swift
public static func addMonitor(monitor: StatsMonitor)
-
Property to compute a snapshot.
Usage Example:
The example below shows how to compute a Hystrix compliant snapshot of the statistics for a given CircuitBreaker instance.
let breaker = CircuitBreaker(name: "Circuit1", command: myFunction, fallback: myFallback) breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: "Something went wrong.") let snapshot = breaker.snapshot
Declaration
Swift
public var snapshot: Snapshot