@@ -51,10 +51,12 @@ func (l *RateLimiter) GetShard(key int64) *Shard {
5151 return l .shards [uint64 (key )% SHARDS ]
5252}
5353
54- func (l * RateLimiter ) Reduce (key string , maxTokens int64 , refillTime int64 , refillAmount int64 , tokens int64 ) (int64 , error ) {
55- defer TimeTrack (time .Now (), "RateLimiter.Reduce" )
54+ func (l * RateLimiter ) getKey (key string , maxTokens int64 , refillTime int64 , refillAmount int64 ) string {
55+ return fmt .Sprintf ("%s:%d:%d:%d" , key , maxTokens , refillTime , refillAmount )
56+ }
5657
57- key = fmt .Sprintf ("%s:%d:%d:%d" , key , maxTokens , refillTime , refillAmount )
58+ func (l * RateLimiter ) Reduce (key string , maxTokens int64 , refillTime int64 , refillAmount int64 , tokens int64 ) (int64 , error ) {
59+ key = l .getKey (key , maxTokens , refillTime , refillAmount )
5860
5961 h := int64 (hash (key ))
6062
@@ -67,8 +69,6 @@ func (l *RateLimiter) Reduce(key string, maxTokens int64, refillTime int64, refi
6769
6870 now := time .Now ().Unix ()
6971
70- log .Debug ().Msgf ("Bucket for key: %s found: %t now %d: %+v" , key , ok , now , bucket )
71-
7272 if ! ok {
7373 value := maxTokens - tokens
7474 tokensNeeded := maxTokens - value
@@ -119,7 +119,6 @@ func (l *RateLimiter) Len() int64 {
119119}
120120
121121func (l * RateLimiter ) CleanUpFullBuckets () {
122-
123122 for _ , shard := range l .shards {
124123 shard .mu .Lock ()
125124 for key , bucket := range shard .Buckets {
@@ -143,7 +142,9 @@ func (l *RateLimiter) StartCleanUpFullBuckets() {
143142 }
144143}
145144
146- func (l * RateLimiter ) Remove (key string ) {
145+ func (l * RateLimiter ) Remove (key string , maxTokens int64 , refillTime int64 , refillAmount int64 ) {
146+ key = l .getKey (key , maxTokens , refillTime , refillAmount )
147+
147148 h := int64 (hash (key ))
148149 shard := l .GetShard (h )
149150
0 commit comments