Skip to content

Commit

Permalink
feat: add user input check on threshold (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
obitech committed Jul 2, 2024
1 parent 7c9d5ed commit 0a46591
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@

/go.work*

*.bench
*.bench

.idea/
10 changes: 10 additions & 0 deletions breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (e *EWMABreaker) apply(o *options) error {
if o.halfOpenDelay == 0 {
return fmt.Errorf("EWMABreaker requires a half-open delay")
}

if e.threshold < 0 || e.threshold > 1 {
return fmt.Errorf("EWMABreaker threshold must be between 0 and 1")
}

return nil
}

Expand Down Expand Up @@ -222,6 +227,11 @@ func (s *SlidingWindowBreaker) apply(o *options) error {
if o.halfOpenDelay == 0 || o.halfOpenDelay > s.windowSize {
o.halfOpenDelay = s.windowSize
}

if s.threshold < 0 || s.threshold > 1 {
return fmt.Errorf("SlidingWindowBreaker threshold must be between 0 and 1")
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func IgnoreContextCanceled(err error) bool {
return err != nil && !errors.Is(err, context.Canceled)
}

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

0 comments on commit 0a46591

Please sign in to comment.