mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-29 00:25:20 +03:00
Update readme
This commit is contained in:
parent
9cb8114cce
commit
560478d5cf
@ -6,7 +6,7 @@ wires to heat up and combust, the circuit breaker design pattern is a fail-first
|
|||||||
mechanism that shuts down the circuit, request/response relationship or a
|
mechanism that shuts down the circuit, request/response relationship or a
|
||||||
service in the case of software development, to prevent bigger failures.
|
service in the case of software development, to prevent bigger failures.
|
||||||
|
|
||||||
**Note:** The words "circuit" and "service" are used synonymously throught this
|
**Note:** The words "circuit" and "service" are used synonymously through this
|
||||||
document.
|
document.
|
||||||
|
|
||||||
## Implementation
|
## Implementation
|
||||||
@ -49,7 +49,7 @@ Circuit is wrapped using the `circuit.Breaker` closure that keeps an internal op
|
|||||||
It returns a fast error if the circuit has failed consecutively more than the specified threshold.
|
It returns a fast error if the circuit has failed consecutively more than the specified threshold.
|
||||||
After a while it retries the request and records it.
|
After a while it retries the request and records it.
|
||||||
|
|
||||||
**Note:** Context type is used here to carry deadlines, cancelation signals, and
|
**Note:** Context type is used here to carry deadlines, cancellation signals, and
|
||||||
other request-scoped values across API boundaries and between processes.
|
other request-scoped values across API boundaries and between processes.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -68,11 +68,11 @@ func Breaker(c Circuit, failureThreshold uint32) Circuit {
|
|||||||
return func(ctx context) error {
|
return func(ctx context) error {
|
||||||
if cnt.ConsecutiveFailures() >= failureThreshold {
|
if cnt.ConsecutiveFailures() >= failureThreshold {
|
||||||
canRetry := func(cnt Counter) {
|
canRetry := func(cnt Counter) {
|
||||||
backoffLevel := Cnt.ConsecutiveFailures() - failureThreshold
|
backOffLevel := Cnt.ConsecutiveFailures() - failureThreshold
|
||||||
|
|
||||||
// Calculates when should the circuit breaker resume propagating requests
|
// Calculates when should the circuit breaker resume propagating requests
|
||||||
// to the service
|
// to the service
|
||||||
shouldRetryAt := cnt.LastActivity().Add(time.Seconds * 2 << backoffLevel)
|
shouldRetryAt := cnt.LastActivity().Add(time.Seconds * 2 << backOffLevel)
|
||||||
|
|
||||||
return time.Now().After(shouldRetryAt)
|
return time.Now().After(shouldRetryAt)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user