-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgbot_rate_limiter.go
40 lines (31 loc) · 1.12 KB
/
tgbot_rate_limiter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package tgo
import (
"context"
"fmt"
"strconv"
"time"
"github.com/nekomeowww/tgo/pkg/redis"
)
func (b *BotAPI) couldCountRateLimitFor(key string, rate int64, perDuration time.Duration) (int64, bool, error) {
if perDuration <= 0 {
return 0, true, nil
}
countedRateStr, err := b.ttlcache.Get(context.Background(), key)
if err != nil {
return 0, false, err
}
countedRate, _ := strconv.ParseInt(countedRateStr.OrEmpty(), 10, 64)
if countedRate >= rate {
return countedRate, false, nil
}
countedRate++
err = b.ttlcache.Set(context.Background(), key, fmt.Sprintf("%d", countedRate), time.Duration(int64(perDuration/time.Second))*time.Second)
if err != nil {
return countedRate, false, err
}
return countedRate, true, nil
}
func (b *BotAPI) RateLimitForCommand(chatID int64, command string, rate int64, perDuration time.Duration) (int64, bool, error) {
// TODO: telegram is static and hardcoded, should be dynamic and constant with specific type when integrated more platforms
return b.couldCountRateLimitFor(redis.CommandRateLimitLock2.Format(command, "telegram", strconv.FormatInt(chatID, 10)), rate, perDuration)
}