Skip to content

Commit

Permalink
CR fixes: improved godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
segevda committed Dec 31, 2024
1 parent 52b65ca commit 989054a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
30 changes: 16 additions & 14 deletions circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
)

// CircuitBreaker can be in one of three states: Closed, Open, or Half-Open.
// When the circuit breaker is Closed, requests are allowed to pass through.
// If a failure count threshold is reached within a specified time-frame, the circuit breaker transitions to the Open state.
// When the circuit breaker is Open, requests are blocked.
// After a specified timeout, the circuit breaker transitions to the Half-Open state.
// When the circuit breaker is Half-Open, a single request is allowed to pass through.
// 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.
// - When the CircuitBreaker is Closed, requests are allowed to pass through.
// - If a failure count threshold is reached within a specified time-frame,
// the CircuitBreaker transitions to the Open state.
// - When the CircuitBreaker is Open, requests are blocked.
// - After a specified timeout, the CircuitBreaker transitions to the Half-Open state.
// - When the CircuitBreaker is Half-Open, a single request is allowed to pass through.
// - If that request fails, the CircuitBreaker returns to the Open state.
// - If the number of successes reaches a specified threshold,
// the CircuitBreaker transitions to the Closed state.
type CircuitBreaker struct {
policies []CircuitBreakerPolicy
timeout time.Duration
Expand All @@ -25,7 +27,7 @@ type CircuitBreaker struct {
lastFail time.Time
}

// NewCircuitBreaker creates a new CircuitBreaker with default settings.
// NewCircuitBreaker creates a new [CircuitBreaker] with default settings.
// The default settings are:
// - Timeout: 10 seconds
// - FailThreshold: 3
Expand All @@ -42,36 +44,36 @@ func NewCircuitBreaker() *CircuitBreaker {
return cb
}

// SetPolicies sets the CircuitBreakerPolicy's that the CircuitBreaker will use to determine whether a response is a failure.
// 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
}

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

// SetFailThreshold 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) SetFailThreshold(threshold uint32) *CircuitBreaker {
cb.failThreshold = threshold
return cb
}

// SetSuccessThreshold 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) SetSuccessThreshold(threshold uint32) *CircuitBreaker {
cb.successThreshold = threshold
return cb
}

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

// CircuitBreaker5xxPolicy is a CircuitBreakerPolicy that trips the circuit breaker if the response status code is 500 or greater
// CircuitBreaker5xxPolicy is a [CircuitBreakerPolicy] that trips the [CircuitBreaker] if the response status code is 500 or greater.
func CircuitBreaker5xxPolicy(resp *http.Response) bool {
return resp.StatusCode > 499
}
Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
module resty.dev/v3

go 1.21
go 1.22.0

require golang.org/x/net v0.27.0
toolchain go1.23.2

require golang.org/x/net v0.32.0

require (
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/tools v0.28.0 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=

0 comments on commit 989054a

Please sign in to comment.