Skip to content

Commit

Permalink
renamete Period for Interval (rate limit)
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Aug 20, 2024
1 parent d0fed6c commit e7f99dd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ const DefaultValues = `
FullMatchRules = []
[Etherman.Validium.RateLimit]
NumRequests = 900
Period = "1s"
Interval = "1s"
`
12 changes: 6 additions & 6 deletions utils/rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (

type RateLimitConfig struct {
NumRequests int
Period types.Duration
Interval types.Duration
}

func NewRateLimitConfig(numRequests int, period time.Duration) RateLimitConfig {
return RateLimitConfig{
NumRequests: numRequests,
Period: types.Duration{Duration: period},
Interval: types.Duration{Duration: period},
}
}

func (r RateLimitConfig) String() string {
return fmt.Sprintf("RateLimitConfig{NumRequests: %d, Period: %s}", r.NumRequests, r.Period)
return fmt.Sprintf("RateLimitConfig{NumRequests: %d, Period: %s}", r.NumRequests, r.Interval)
}

func (r RateLimitConfig) Enabled() bool {
return r.NumRequests > 0 && r.Period.Duration > 0
return r.NumRequests > 0 && r.Interval.Duration > 0
}

type RateLimit struct {
Expand All @@ -52,7 +52,7 @@ func (r *RateLimit) Call(msg string, allowToSleep bool) *time.Duration {
now := r.timeProvider.Now()
r.cleanOutdatedCalls(now)
if len(r.bucket) >= r.cfg.NumRequests {
sleepTime := r.cfg.Period.Duration - r.timeProvider.Now().Sub(r.bucket[0])
sleepTime := r.cfg.Interval.Duration - r.timeProvider.Now().Sub(r.bucket[0])
if allowToSleep {
if msg != "" {
log.Debugf("Rate limit reached, sleeping for %s for %s", sleepTime, msg)
Expand All @@ -71,7 +71,7 @@ func (r *RateLimit) Call(msg string, allowToSleep bool) *time.Duration {
func (r *RateLimit) cleanOutdatedCalls(now time.Time) {
for i, call := range r.bucket {
diff := now.Sub(call)
if diff < r.cfg.Period.Duration {
if diff < r.cfg.Interval.Duration {
r.bucket = r.bucket[i:]
return
}
Expand Down

0 comments on commit e7f99dd

Please sign in to comment.