Skip to content

Commit

Permalink
Demonstrate that maybeRefresh is never called for these dynamic rate …
Browse files Browse the repository at this point in the history
…limiters
  • Loading branch information
MichaelSnowden committed Sep 30, 2023
1 parent bba21ca commit 9578140
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
28 changes: 28 additions & 0 deletions common/quotas/dynamic_rate_limiter_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package quotas

import (
"context"
"fmt"
"time"
)

Expand All @@ -43,22 +44,43 @@ type (

refreshTimer *time.Timer
rateLimiter *RateLimiterImpl
name string
}

params struct {
name string
}

DynamicRateLimiterOption func(p *params)
)

var _ RateLimiter = (*DynamicRateLimiterImpl)(nil)

func WithName(name string) DynamicRateLimiterOption {
return func(p *params) {
p.name = name
}
}

// NewDynamicRateLimiter returns a rate limiter which handles dynamic config
func NewDynamicRateLimiter(
rateBurstFn RateBurst,
refreshInterval time.Duration,
opts ...DynamicRateLimiterOption,
) *DynamicRateLimiterImpl {
p := params{
name: "",
}
for _, opt := range opts {
opt(&p)
}
rateLimiter := &DynamicRateLimiterImpl{
rateBurstFn: rateBurstFn,
refreshInterval: refreshInterval,

refreshTimer: time.NewTimer(refreshInterval),
rateLimiter: NewRateLimiter(rateBurstFn.Rate(), rateBurstFn.Burst()),
name: p.name,
}
return rateLimiter
}
Expand All @@ -67,10 +89,12 @@ func NewDynamicRateLimiter(
// for incoming traffic
func NewDefaultIncomingRateLimiter(
rateFn RateFn,
opts ...DynamicRateLimiterOption,
) *DynamicRateLimiterImpl {
return NewDynamicRateLimiter(
NewDefaultIncomingRateBurst(rateFn),
defaultRefreshInterval,
opts...,
)
}

Expand Down Expand Up @@ -138,6 +162,10 @@ func (d *DynamicRateLimiterImpl) Refresh() {
}

func (d *DynamicRateLimiterImpl) maybeRefresh() {
if d.name != "" {
panic(fmt.Sprintf("maybeRefresh was called on a DynamicRateLimiterImpl with a name: %q", d.name))
}

select {
case <-d.refreshTimer.C:
d.refreshTimer.Reset(d.refreshInterval)
Expand Down
20 changes: 16 additions & 4 deletions service/frontend/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,22 @@ func RateLimitInterceptorProvider(

return interceptor.NewRateLimitInterceptor(
configs.NewRequestToRateLimiter(
quotas.NewDefaultIncomingRateLimiter(rateFn),
quotas.NewDefaultIncomingRateLimiter(rateFn),
quotas.NewDefaultIncomingRateLimiter(namespaceReplicationInducingRateFn),
quotas.NewDefaultIncomingRateLimiter(rateFn),
quotas.NewDefaultIncomingRateLimiter(
rateFn,
quotas.WithName("executionRateBurstFn"),
),
quotas.NewDefaultIncomingRateLimiter(
rateFn,
quotas.WithName("visibilityRateBurstFn"),
),
quotas.NewDefaultIncomingRateLimiter(
namespaceReplicationInducingRateFn,
quotas.WithName("namespaceReplicationInducingRateBurstFn"),
),
quotas.NewDefaultIncomingRateLimiter(
rateFn,
quotas.WithName("otherRateBurstFn"),
),
serviceConfig.OperatorRPSRatio,
),
map[string]int{},
Expand Down

0 comments on commit 9578140

Please # to comment.