-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#163: Extracted health tracking logic into a separate component
- Loading branch information
1 parent
081f5b9
commit ba56d3f
Showing
4 changed files
with
67 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package health | ||
|
||
import ( | ||
"errors" | ||
"glide/pkg/providers/clients" | ||
) | ||
|
||
type HealthTracker struct { | ||
errBudget *TokenBucket | ||
rateLimit *RateLimitTracker | ||
} | ||
|
||
func NewHealthTracker(budget ErrorBudget) *HealthTracker { | ||
return &HealthTracker{ | ||
rateLimit: NewRateLimitTracker(), | ||
errBudget: NewTokenBucket(budget.TimePerTokenMicro(), budget.Budget()), | ||
} | ||
} | ||
|
||
func (t *HealthTracker) Healthy() bool { | ||
return !t.rateLimit.Limited() && t.errBudget.HasTokens() | ||
} | ||
|
||
func (t *HealthTracker) TrackErr(err error) { | ||
var rateLimitErr *clients.RateLimitError | ||
|
||
if errors.As(err, &rateLimitErr) { | ||
t.rateLimit.SetLimited(rateLimitErr.UntilReset()) | ||
|
||
return | ||
} | ||
|
||
_ = t.errBudget.Take(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters