Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Refactor DRAIN_OVERLIMIT to DRAIN_OVER_LIMIT.
Browse files Browse the repository at this point in the history
  • Loading branch information
Baliedge committed Jan 4, 2024
1 parent 0ce98a7 commit 341b8bc
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 62 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ This will reset the rate limit as if created new on first use.

When using Reset Remaining, the `Hits` field should be 0.

## Drain Overlimit Behavior
Users may add behavior `Behavior_DRAIN_OVERLIMIT` to the rate check request. A
`GetRateLimits` call drains the remaining counter on first overlimit event. Then,
successive `GetRateLimits` calls will return zero remaining counter and not any
residual value.
## Drain Over Limit Behavior
Users may add behavior `Behavior_DRAIN_OVER_LIMIT` to the rate check request.
A `GetRateLimits` call drains the remaining counter on first over limit event.
Then, successive `GetRateLimits` calls will return zero remaining counter and
not any residual value.

This facilitates scenarios that require an overlimit event to stay overlimit
This facilitates scenarios that require an over limit event to stay over limit
until the rate limit resets. This approach is necessary if a process must make
two rate checks, before and after a process, and the `Hit` amount is not known
until after the process.
Expand All @@ -193,12 +193,12 @@ until after the process.
`Remaining` counter. If `Remaining` is zero, it's known
that the rate limit is depleted and the process can be aborted.
- After process: Call `GetRateLimits` with a user specified `Hits` value. If
the call returns overlimit, the process cannot be aborted because it had
already completed. Using `DRAIN_OVERLIMIT` behavior, the `Remaining` count
the call returns over limit, the process cannot be aborted because it had
already completed. Using `DRAIN_OVER_LIMIT` behavior, the `Remaining` count
will be drained to zero.

Once an overlimit occurs in the "After" step, successive processes will detect
the overlimit state in the "Before" step.
Once an over limit occurs in the "After" step, successive processes will detect
the over limit state in the "Before" step.

## Gubernator as a library
If you are using golang, you can use Gubernator as a library. This is useful if
Expand Down
8 changes: 4 additions & 4 deletions algorithms.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ func tokenBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq) (resp *
trace.SpanFromContext(ctx).AddEvent("Over the limit")
metricOverLimitCounter.Add(1)
rl.Status = Status_OVER_LIMIT
if HasBehavior(r.Behavior, Behavior_DRAIN_OVERLIMIT) {
// DRAIN_OVERLIMIT behavior drains the remaining counter.
if HasBehavior(r.Behavior, Behavior_DRAIN_OVER_LIMIT) {
// DRAIN_OVER_LIMIT behavior drains the remaining counter.
t.Remaining = 0
rl.Remaining = 0
}
Expand Down Expand Up @@ -399,8 +399,8 @@ func leakyBucket(ctx context.Context, s Store, c Cache, r *RateLimitReq) (resp *
if r.Hits > int64(b.Remaining) {
metricOverLimitCounter.Add(1)
rl.Status = Status_OVER_LIMIT
if HasBehavior(r.Behavior, Behavior_DRAIN_OVERLIMIT) {
// DRAIN_OVERLIMIT behavior drains the remaining counter.
if HasBehavior(r.Behavior, Behavior_DRAIN_OVER_LIMIT) {
// DRAIN_OVER_LIMIT behavior drains the remaining counter.
b.Remaining = 0
rl.Remaining = 0
}
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type BehaviorConfig struct {
GlobalBatchLimit int
// ForceGlobal forces global behavior on all rate limit checks.
ForceGlobal bool
// ForceDrainOverlimit forces drain overlimit behavior on all rate limit checks.
ForceDrainOverlimit bool
// ForceDrainOverLimit forces "drain over limit" behavior on all rate limit checks.
ForceDrainOverLimit bool

// Number of concurrent requests that will be made to peers. Defaults to 100
GlobalPeerRequestsConcurrency int
Expand Down
8 changes: 4 additions & 4 deletions functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func TestTokenBucketNegativeHits(t *testing.T) {
}
}

func TestDrainOverlimit(t *testing.T) {
func TestDrainOverLimit(t *testing.T) {
defer clock.Freeze(clock.Now()).Unfreeze()
client, errs := guber.DialV1Server(cluster.PeerAt(0).GRPCAddress, nil)
require.Nil(t, errs)
Expand All @@ -393,7 +393,7 @@ func TestDrainOverlimit(t *testing.T) {
Remaining: 9,
Status: guber.Status_UNDER_LIMIT,
}, {
Name: "overlimit hit",
Name: "over limit hit",
Hits: 100,
Remaining: 0,
Status: guber.Status_OVER_LIMIT,
Expand All @@ -413,10 +413,10 @@ func TestDrainOverlimit(t *testing.T) {
resp, err := client.GetRateLimits(ctx, &guber.GetRateLimitsReq{
Requests: []*guber.RateLimitReq{
{
Name: "test_drain_overlimit",
Name: "test_drain_over_limit",
UniqueKey: fmt.Sprintf("account:1234:%d", idx),
Algorithm: algoCase.Algorithm,
Behavior: guber.Behavior_DRAIN_OVERLIMIT,
Behavior: guber.Behavior_DRAIN_OVER_LIMIT,
Duration: guber.Second * 30,
Hits: test.Hits,
Limit: 10,
Expand Down
4 changes: 2 additions & 2 deletions gubernator.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ func (s *V1Instance) GetRateLimits(ctx context.Context, r *GetRateLimitsReq) (*G
if s.conf.Behaviors.ForceGlobal {
SetBehavior(&req.Behavior, Behavior_GLOBAL, true)
}
if s.conf.Behaviors.ForceDrainOverlimit {
SetBehavior(&req.Behavior, Behavior_DRAIN_OVERLIMIT, true)
if s.conf.Behaviors.ForceDrainOverLimit {
SetBehavior(&req.Behavior, Behavior_DRAIN_OVER_LIMIT, true)
}

peer, err = s.GetPeer(ctx, key)
Expand Down
64 changes: 32 additions & 32 deletions gubernator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gubernator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ enum Behavior {
// least 2 instances of Gubernator.
MULTI_REGION = 16;

// A GetRateLimits call drains the remaining counter on first overlimit
// A GetRateLimits call drains the remaining counter on first over limit
// event. Then, successive GetRateLimits calls will return zero remaining
// counter and not any residual value.
DRAIN_OVERLIMIT = 32;
DRAIN_OVER_LIMIT = 32;

// TODO: Add support for LOCAL. Which would force the rate limit to be handled by the local instance
}
Expand Down
Loading

0 comments on commit 341b8bc

Please sign in to comment.