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

Commit

Permalink
Fix rate limiter panic (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
black-adder authored Mar 23, 2017
1 parent 54d9b0c commit 8cacc0e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion utils/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@

package utils

import "time"
import (
"sync"
"time"
)

// RateLimiter is a filter used to check if a message that is worth itemCost units is within the rate limits.
type RateLimiter interface {
CheckCredit(itemCost float64) bool
}

type rateLimiter struct {
sync.Mutex

creditsPerSecond float64
balance float64
maxBalance float64
Expand Down Expand Up @@ -58,6 +63,8 @@ func NewRateLimiter(creditsPerSecond, maxBalance float64) RateLimiter {
}

func (b *rateLimiter) CheckCredit(itemCost float64) bool {
b.Lock()
defer b.Unlock()
// calculate how much time passed since the last tick, and update current tick
currentTime := b.timeNow()
elapsedTime := currentTime.Sub(b.lastTick)
Expand Down

0 comments on commit 8cacc0e

Please sign in to comment.