diff --git a/circuit_breaker.go b/circuit_breaker.go index 534df3a..fac650c 100644 --- a/circuit_breaker.go +++ b/circuit_breaker.go @@ -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 @@ -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 @@ -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 } diff --git a/go.mod b/go.mod index b575af9..1267bc5 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index 7566714..d952f47 100644 --- a/go.sum +++ b/go.sum @@ -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=