You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*The CircuitBreaker state is based on timeouts only.*
40
+
*In this form of usage, the CircuitBreaker state is based on timeouts only.*
41
+
42
+
If the function you are circuit breaking makes an asynchronous call(s) and the execution time of that call should be taking into account, then see [`Advanced Usage`](#advanced-usage) below.
41
43
42
44
1. Define a fallback function with the signature `(<BreakerError, (fallbackArg1, fallbackArg2,...)>) -> Void`:
43
45
```swift
44
-
functestFallback (error: Bool, msg: String) {
46
+
funcmyFallback (error: Bool, msg: String) {
45
47
// The fallback will be called if the request does not return before the specified timeout
46
48
// or if the CircuitBreaker is currently in Open state and set to fail fast.
47
-
// Client is expected to use the fallback to do alternate processing, such as show an error page.
49
+
// Client code can use the fallback function to do alternate processing, such as show an error page.
let breaker =CircuitBreaker(fallback: testFallback, command: testEndpoint)
144
-
145
-
breaker.run(commandArgs: (input : "testInput1", { data, err in
146
-
if err {
147
-
print(err)
148
-
} else {
149
-
print(data)
150
-
}
151
-
}), fallbackArgs: (msg: "Something went wrong."))
99
+
let breaker =CircuitBreaker(fallback: myFallback, command: myFunction)
152
100
153
-
breaker.run(commandArgs: (input : "testInput2", { data, err in
154
-
if err {
155
-
print(err)
156
-
} else {
157
-
print(data)
158
-
}
159
-
}), fallbackArgs: (msg: "Something went wrong."))
101
+
breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: (msg: "Something went wrong."))
102
+
breaker.run(commandArgs: (a: 15, b: 35), fallbackArgs: (msg: "Something went wrong."))
160
103
161
104
...
162
105
```
163
106
164
-
### Advanced Usage:
107
+
### Advanced Usage
165
108
166
-
*The CircuitBreaker state is based on timeouts and user defined failures.*
109
+
*In this form of usage, the CircuitBreaker state is based on timeouts and user defined failures (quite useful when the function you are circuit breaking makes an asynchronous call).*
167
110
168
111
1. Define a fallback function with the signature `(<BreakerError, (fallbackArg1, fallbackArg2,...)>) -> Void`:
*`timeout` Amount in seconds that the request should complete before. Default is set to 10 seconds.
258
-
*`resetTimeout` Amount in seconds to wait before setting to halfopen state. Default is set to 60 seconds.
259
-
*`maxFailures` Number of failures allowed before setting state to open. Default is set to 5.
236
+
*`timeout` Amount in milliseconds that your function should complete before the invocation is considered a failure. Default is set to 1000 milliseconds.
237
+
*`resetTimeout` Amount in milliseconds to wait before setting to halfopen state. Default is set to 60000 milliseconds.
238
+
*`maxFailures` Number of failures allowed within `rollingWindow` before setting state to open. Default is set to 5.
239
+
*`rollingWindow` Time window in milliseconds where the maximum number of failures must occur to trip the circuit. For instance, say `maxFailures` is 5 and `rollingWindow` is 10000 milliseconds. In such 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.
260
240
*`bulkhead` Number of the limit of concurrent requests running at one time. Default is set to 0, which is equivalent to not using the bulkheading feature.
261
241
*`fallback` Function user specifies to signal timeout or fastFail completion. Required format: `(BreakerError, (fallbackArg1, fallbackArg2,...)) -> Void`
*`timeout` Amount in seconds that the request should complete before. Default is set to 10 seconds.
248
+
*`timeout` Amount in seconds that the request should complete before the invocation is considered a failure. Default is set to 1 second.
269
249
*`resetTimeout` Amount in seconds to wait before setting to halfopen state. Default is set to 60 seconds.
270
-
*`maxFailures` Number of failures allowed before setting state to open. Default is set to 5.
250
+
*`maxFailures` Number of failures allowed within `rollingWindow` before setting state to open. Default is set to 5.
251
+
*`rollingWindow` Time window in milliseconds where the maximum number of failures must occur to trip the circuit. For instance, say `maxFailures` is 5 and `rollingWindow` is 10000 milliseconds. In such 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.
271
252
*`bulkhead` Number of the limit of concurrent requests running at one time. Default is set to 0, which is equivalent to not using the bulkheading feature.
272
253
*`fallback` Function user specifies to signal timeout or fastFail completion. Required format: `(BreakerError, (fallbackArg1, fallbackArg2,...)) -> Void`
273
-
*`commandWrapper` Invocation wrapper around endpoint name to circuit break, allows user defined failures.
254
+
*`commandWrapper` Invocation wrapper around logic to circuit break, allows user defined failures (provides reference to circuit breaker instance).
274
255
275
256
### Stats
276
257
```swift
277
258
...
278
259
// Create CircuitBreaker
279
-
let breaker =CircuitBreaker(fallback: tesFallback, command: testEndpoint)
260
+
let breaker =CircuitBreaker(fallback: myFallback, command: myFunction)
280
261
281
262
// Invoke breaker call
282
-
breaker.run(args: (input: "test"))
263
+
breaker.run(commandArgs: (a: 10, b: 20), fallbackArgs: (msg: "Something went wrong."))
0 commit comments