/
RateLimitError.go
73 lines (57 loc) · 1.45 KB
/
RateLimitError.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
package goboot
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)
type RateLimitError struct {
total int
remaining int
retryAfter string
}
func NewRateLimitError(data map[string]interface{}) RateLimitError {
var total int
if n1, ok := data["total"].(int); ok && n1 > 0 {
total = n1
}
var remaining int
if n1, ok := data["remaining"].(int); ok && n1 > 0 {
remaining = n1
}
var retryAfter string
if s1, ok := data["retryAfter"].(string); ok && s1 != "" {
retryAfter = s1
}
return RateLimitError{
total: total,
remaining: remaining,
retryAfter: retryAfter,
}
}
func (ex RateLimitError) Error() string {
return "rate limit exceed"
}
func (ex RateLimitError) Total() int {
return ex.total
}
func (ex RateLimitError) Remaining() int {
return ex.remaining
}
func (ex RateLimitError) RetryAfter() string {
return ex.retryAfter
}
func (ex RateLimitError) AddSpecifyHeaders(arg0 interface{}) {
if ctx, ok := arg0.(*fiber.Ctx); ok {
ctx.Set("X-Ratelimit-Limit", fmt.Sprintf("%d", ex.Total()))
ctx.Set("X-Ratelimit-Remaining", fmt.Sprintf("%d", ex.Remaining()))
if ex.RetryAfter() != "" {
ctx.Set("Retry-After", ex.RetryAfter())
}
} else if ctx, ok := arg0.(*gin.Context); ok {
ctx.Header("X-Ratelimit-Limit", fmt.Sprintf("%d", ex.Total()))
ctx.Header("X-Ratelimit-Remaining", fmt.Sprintf("%d", ex.Remaining()))
if ex.RetryAfter() != "" {
ctx.Header("Retry-After", ex.RetryAfter())
}
}
}