Skip to content

Commit

Permalink
CR fixes: renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
segevda committed Dec 30, 2024
1 parent ee36a70 commit 7884ad5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
32 changes: 16 additions & 16 deletions breaker.go → circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// If that request fails, the circuit breaker returns to the Open state.
// If SuccessThreshold requests succeed, the circuit breaker transitions back to the Closed state.
type CircuitBreaker struct {
policies []BreakerPolicy
policies []CircuitBreakerPolicy
timeout time.Duration
failThreshold, successThreshold int

Expand All @@ -33,48 +33,48 @@ type CircuitBreaker struct {
// - Policies: Count5xxPolicy
func NewCircuitBreaker() *CircuitBreaker {
return &CircuitBreaker{
policies: []BreakerPolicy{Count5xxPolicy},
policies: []CircuitBreakerPolicy{Count5xxPolicy},
timeout: 10 * time.Second,
failThreshold: 3,
successThreshold: 1,
}
}

// Policies sets the BreakerPolicy's that the CircuitBreaker will use to determine whether a response is a failure.
func (cb *CircuitBreaker) Policies(policies []BreakerPolicy) *CircuitBreaker {
// SetPolicies sets the CircuitBreakerPolicy's that the CircuitBreaker will use to determine whether a response is a failure.
func (cb *CircuitBreaker) SetPolicies(policies []CircuitBreakerPolicy) *CircuitBreaker {
cb.policies = policies
return cb
}

// Timeout sets the timeout duration for the CircuitBreaker
func (cb *CircuitBreaker) Timeout(timeout time.Duration) *CircuitBreaker {
// SetTimeout sets the timeout duration for the CircuitBreaker
func (cb *CircuitBreaker) SetTimeout(timeout time.Duration) *CircuitBreaker {
cb.timeout = timeout
return cb
}

// FailThreshold sets the number of failures that must occur within the timeout duration for the CircuitBreaker to
// SetFailThreshold sets the number of failures that must occur within the timeout duration for the CircuitBreaker to
// transition to the Open state.
func (cb *CircuitBreaker) FailThreshold(threshold int) *CircuitBreaker {
func (cb *CircuitBreaker) SetFailThreshold(threshold int) *CircuitBreaker {
cb.failThreshold = threshold
return cb
}

// SuccessThreshold sets the number of successes that must occur to transition the CircuitBreaker from the Half-Open state
// SetSuccessThreshold sets the number of successes that must occur to transition the CircuitBreaker from the Half-Open state
// to the Closed state.
func (cb *CircuitBreaker) SuccessThreshold(threshold int) *CircuitBreaker {
func (cb *CircuitBreaker) SetSuccessThreshold(threshold int) *CircuitBreaker {
cb.successThreshold = threshold
return cb
}

// BreakerPolicy is a function that determines whether a response should trip the circuit breaker
type BreakerPolicy func(resp *http.Response) bool
// CircuitBreakerPolicy is a function that determines whether a response should trip the circuit breaker
type CircuitBreakerPolicy func(resp *http.Response) bool

// Count5xxPolicy is a BreakerPolicy that trips the circuit breaker if the response status code is 500 or greater
// Count5xxPolicy is a CircuitBreakerPolicy that trips the circuit breaker if the response status code is 500 or greater
func Count5xxPolicy(resp *http.Response) bool {
return resp.StatusCode > 499
}

var ErrBreakerOpen = errors.New("circuit breaker open")
var ErrCircuitBreakerOpen = errors.New("rest: circuit breaker open")

type circuitBreakerState uint32

Expand All @@ -94,13 +94,13 @@ func (cb *CircuitBreaker) allow() error {
}

if cb.getState() == open {
return ErrBreakerOpen
return ErrCircuitBreakerOpen
}

return nil
}

func (cb *CircuitBreaker) processResponse(resp *http.Response) {
func (cb *CircuitBreaker) applyPolicies(resp *http.Response) {
if cb == nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2135,7 +2135,7 @@ func (c *Client) execute(req *Request) (*Response, error) {
}
}
if resp != nil {
c.circuitBreaker.processResponse(resp)
c.circuitBreaker.applyPolicies(resp)

response.Body = resp.Body
if err = response.wrapContentDecompresser(); err != nil {
Expand Down
12 changes: 7 additions & 5 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,8 @@ func TestClientDebugf(t *testing.T) {
})
}

var _ CircuitBreakerPolicy = Count5xxPolicy

func TestClientCircuitBreaker(t *testing.T) {
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
t.Logf("Method: %v", r.Method)
Expand All @@ -1437,17 +1439,17 @@ func TestClientCircuitBreaker(t *testing.T) {

c := dcnl().SetCircuitBreaker(
NewCircuitBreaker().
Timeout(timeout).
FailThreshold(failThreshold).
SuccessThreshold(successThreshold).
Policies([]BreakerPolicy{Count5xxPolicy}))
SetTimeout(timeout).
SetFailThreshold(failThreshold).
SetSuccessThreshold(successThreshold).
SetPolicies([]CircuitBreakerPolicy{Count5xxPolicy}))

for i := 0; i < failThreshold; i++ {
_, err := c.R().Get(ts.URL + "/500")
assertNil(t, err)
}
resp, err := c.R().Get(ts.URL + "/500")
assertErrorIs(t, ErrBreakerOpen, err)
assertErrorIs(t, ErrCircuitBreakerOpen, err)
assertNil(t, resp)
assertEqual(t, c.circuitBreaker.getState(), open)

Expand Down

0 comments on commit 7884ad5

Please sign in to comment.