Skip to content

Commit 0a46591

Browse files
authored
feat: add user input check on threshold (#17)
1 parent 7c9d5ed commit 0a46591

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313

1414
/go.work*
1515

16-
*.bench
16+
*.bench
17+
18+
.idea/

breaker.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ func (e *EWMABreaker) apply(o *options) error {
129129
if o.halfOpenDelay == 0 {
130130
return fmt.Errorf("EWMABreaker requires a half-open delay")
131131
}
132+
133+
if e.threshold < 0 || e.threshold > 1 {
134+
return fmt.Errorf("EWMABreaker threshold must be between 0 and 1")
135+
}
136+
132137
return nil
133138
}
134139

@@ -222,6 +227,11 @@ func (s *SlidingWindowBreaker) apply(o *options) error {
222227
if o.halfOpenDelay == 0 || o.halfOpenDelay > s.windowSize {
223228
o.halfOpenDelay = s.windowSize
224229
}
230+
231+
if s.threshold < 0 || s.threshold > 1 {
232+
return fmt.Errorf("SlidingWindowBreaker threshold must be between 0 and 1")
233+
}
234+
225235
return nil
226236
}
227237

options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func IgnoreContextCanceled(err error) bool {
4444
return err != nil && !errors.Is(err, context.Canceled)
4545
}
4646

47-
// WithMiddleware allows wrapping the [Breaker] via a [BreakerMiddleware].
47+
// WithBreakerMiddleware allows wrapping the [Breaker] via a [BreakerMiddleware].
4848
// Middlewares are processed from innermost to outermost, meaning the first added middleware is the closest to the
4949
// wrapped function.
5050
// ⚠️ This means ordering is significant: since "outer" middleware may react differently depending on the output of

0 commit comments

Comments
 (0)