-
Notifications
You must be signed in to change notification settings - Fork 2
/
ratelimiter_handler.go
91 lines (82 loc) · 2.23 KB
/
ratelimiter_handler.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 logging
import (
"context"
"fmt"
"log/slog"
"sync/atomic"
"time"
"golang.org/x/time/rate"
)
func NewRateLimiterHandler(ctx context.Context, next slog.Handler, cfg RateLimiterConfig) slog.Handler {
droppedLogsCounters := map[slog.Level]*atomic.Uint64{
slog.LevelDebug: {},
slog.LevelInfo: {},
slog.LevelWarn: {},
slog.LevelError: {},
}
logsRate := cfg.Limit
burst := cfg.Burst
if cfg.Inform {
go printDroppedLogsCounter(ctx, droppedLogsCounters)
}
return &RateLimiterHandler{
next: next,
rt: map[slog.Level]*rate.Limiter{
slog.LevelDebug: rate.NewLimiter(logsRate, burst),
slog.LevelInfo: rate.NewLimiter(logsRate, burst),
slog.LevelWarn: rate.NewLimiter(logsRate, burst),
slog.LevelError: rate.NewLimiter(logsRate, burst),
},
droppedLogsCounters: droppedLogsCounters,
}
}
type RateLimiterHandler struct {
next slog.Handler
rt map[slog.Level]*rate.Limiter
droppedLogsCounters map[slog.Level]*atomic.Uint64
}
func (s *RateLimiterHandler) Enabled(ctx context.Context, level slog.Level) bool {
if !s.next.Enabled(ctx, level) {
return false
}
if !s.rt[level].Allow() {
s.droppedLogsCounters[level].Add(1)
return false
}
return true
}
func (s *RateLimiterHandler) Handle(ctx context.Context, record slog.Record) error {
return s.next.Handle(ctx, record)
}
func (s *RateLimiterHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &RateLimiterHandler{
next: s.next.WithAttrs(attrs),
rt: s.rt,
droppedLogsCounters: s.droppedLogsCounters,
}
}
func (s *RateLimiterHandler) WithGroup(name string) slog.Handler {
return &RateLimiterHandler{
next: s.next.WithGroup(name),
rt: s.rt,
droppedLogsCounters: s.droppedLogsCounters,
}
}
func printDroppedLogsCounter(ctx context.Context, droppedLogsCounters map[slog.Level]*atomic.Uint64) {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
for level, val := range droppedLogsCounters {
count := val.Load()
if count > 0 {
slog.Warn(fmt.Sprintf("logs rate limit, dropped %d lines for level %s", count, level.String()))
val.Store(0)
}
}
}
}
}