-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimiter.go
52 lines (42 loc) · 1.15 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
package rlimit
import (
"errors"
"time"
"github.com/valsov/rlimit/storage"
)
type Limiter[TConfig, TValue any] interface {
TryAllow(count int, config TConfig, userValue TValue, nowUtc time.Time) (bool, TValue)
}
type RateLimiter[TConfig, TValue any] struct {
store storage.Storage[TConfig, TValue]
GlobalConfig TConfig
InternalLimiter Limiter[TConfig, TValue]
}
func (r *RateLimiter[TConfig, TValue]) GlobalConfigure(config TConfig) {
r.GlobalConfig = config
}
func (r *RateLimiter[TConfig, TValue]) Configure(id string, config TConfig) error {
data, err := r.store.Get(id)
if err != nil {
return err
}
data.Config = config
return r.store.Set(id, data)
}
func (r *RateLimiter[TConfig, TValue]) TryAllow(id string, count int) (bool, error) {
if count <= 0 {
return false, errors.New("count must be greater than 0")
}
data, err := r.store.Get(id)
if err != nil {
return false, nil
}
config := data.Config
if !data.HasConfig {
config = r.GlobalConfig
}
allowed, newUserValue := r.InternalLimiter.TryAllow(count, config, data.Value, time.Now().UTC())
data.Value = newUserValue
err = r.store.Set(id, data)
return allowed, err
}