forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrottle.go
91 lines (81 loc) · 2.21 KB
/
throttle.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package middleware
import (
"chat/utils"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/spf13/viper"
"strings"
)
type Limiter struct {
Duration int
Count int64
}
func (l *Limiter) RateLimit(client *redis.Client, ip string, path string) (bool, error) {
key := fmt.Sprintf("rate:%s:%s", path, ip)
rate, err := utils.IncrWithLimit(client, key, 1, l.Count, int64(l.Duration))
return !rate, err
}
var limits = map[string]Limiter{
"/login": {Duration: 10, Count: 20},
"/register": {Duration: 120, Count: 10},
"/verify": {Duration: 120, Count: 10},
"/reset": {Duration: 120, Count: 10},
"/apikey": {Duration: 1, Count: 2},
"/resetkey": {Duration: 3600, Count: 3},
"/package": {Duration: 1, Count: 2},
"/quota": {Duration: 1, Count: 2},
"/buy": {Duration: 1, Count: 2},
"/subscribe": {Duration: 1, Count: 2},
"/subscription": {Duration: 1, Count: 2},
"/chat": {Duration: 1, Count: 5},
"/conversation": {Duration: 1, Count: 5},
"/invite": {Duration: 7200, Count: 20},
"/redeem": {Duration: 1200, Count: 60},
"/dashboard": {Duration: 1, Count: 5},
"/card": {Duration: 1, Count: 5},
"/generation": {Duration: 1, Count: 5},
"/article": {Duration: 1, Count: 5},
"/broadcast": {Duration: 1, Count: 2},
}
func GetPrefixMap[T comparable](s string, p map[string]T) *T {
if viper.GetBool("serve_static") {
s = strings.TrimPrefix(s, "/api")
}
for k, v := range p {
if strings.HasPrefix(s, k) {
return &v
}
}
return nil
}
func ThrottleMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
ip := c.ClientIP()
path := c.Request.URL.Path
cache := utils.GetCacheFromContext(c)
limiter := GetPrefixMap[Limiter](path, limits)
if limiter != nil {
rate, err := limiter.RateLimit(cache, ip, path)
if err != nil {
c.JSON(200, gin.H{
"status": false,
"reason": err.Error(),
"error": err.Error(),
})
c.Abort()
return
}
if rate {
c.JSON(200, gin.H{
"status": false,
"reason": "You have sent too many requests. Please try again later.",
"error": "request_throttled",
})
c.Abort()
return
}
}
c.Next()
}
}