@@ -42,6 +42,8 @@ type Breaker interface {
4242 // The halfOpen parameter indicates whether the call was made in half-open state.
4343 // The failure parameter indicates whether the call failed.
4444 observe (halfOpen , failure bool ) stateChange
45+
46+ Option // breakers can also modify or sanity-check their circuit's options
4547}
4648
4749// ObserverFactory is an interface that allows customizing the per-call observer creation.
@@ -87,6 +89,8 @@ func NewCircuit[IN, OUT any](f WrappedFunc[IN, OUT], breaker Breaker, opts ...Op
8789
8890 if breaker != nil {
8991 o .breaker = breaker
92+ // apply breaker as last, so it can verify
93+ opts = append (opts , breaker )
9094 }
9195
9296 // the default observerFactory is the circuit itself
@@ -98,18 +102,8 @@ func NewCircuit[IN, OUT any](f WrappedFunc[IN, OUT], breaker Breaker, opts ...Op
98102 }
99103 }
100104
101- if breakerOpt , ok := breaker .(Option ); ok {
102- if err := breakerOpt .apply (& o ); err != nil {
103- return nil , fmt .Errorf ("applying breaker option: %w" , err )
104- }
105- }
106-
107105 c .options = o
108106
109- if _ , ok := breaker .(* EWMABreaker ); ok && c .halfOpenDelay == 0 {
110- return nil , fmt .Errorf ("EWMABreaker requires a half-open delay" )
111- }
112-
113107 return c , nil
114108}
115109
@@ -297,3 +291,7 @@ type noopBreaker struct{}
297291func (noopBreaker ) observe (halfOpen , failure bool ) stateChange {
298292 return stateChangeNone
299293}
294+
295+ func (noopBreaker ) apply (* options ) error {
296+ return nil
297+ }
0 commit comments