-
-
Notifications
You must be signed in to change notification settings - Fork 726
Description
type Config struct {
MaxRequests uint32 // Max requests allowed in half-open state
Interval time.Duration // Statistical window for closed state
Timeout time.Duration // Time to wait before half-open
ReadyToTrip func(Metrics) bool // Function to determine when to trip
OnStateChange func(name string, from State, to State) // State change callback
}
While implementing the Circuit Breaker challenge, I found some unclear aspects in the Config struct.
For reference, I compared the design to the Azure Circuit Breaker pattern to clarify expected behavior.
Issues
-
MaxRequests - Should be renamed to
HalfOpenMaxRequests. Ifcb.halfOpenRequestsis equal to this value, sayn, the state changes to closed. The errorErrTooManyRequestsdoesn't make sense here; in the half-open state, even if one request fails, the CB moves to the open state. Ifnnumber of requests all succeed, the CB transitions to closed state. At no time, the CB will receive more thannrequests in the half-open state. -
Interval – The comment "Statistical window for closed state" is unclear. It seems to represent the time window used to measure failures in the closed state. Suggest renaming the field and/or updating the comment.
-
Timeout – The period the breaker stays open before moving to half-open. Clear enough.
-
ReadyToTrip – Allowing the client to decide when to trip may lead to abuse (e.g., always returning false). This logic should ideally be internal.