-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratelimit.go
More file actions
59 lines (47 loc) · 1.45 KB
/
ratelimit.go
File metadata and controls
59 lines (47 loc) · 1.45 KB
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
// Copyright (C) 2021-2022 Amuzed GmbH finn@amuzed.io.
// This file is part of the project AMUZED.
// AMUZED can not be copied and/or distributed without the express.
// permission of Amuzed GmbH.
package ratelimit
import (
"sync"
"time"
"github.com/google/uuid"
)
// RateLimiter allows to prevent multiple events in fixed period of time.
type RateLimiter struct {
mu sync.Mutex
rateLimited map[uuid.UUID]time.Time
duration time.Duration
}
// Config defines values needed to start rate limiter.
type Config struct {
LimitForBet time.Duration `json:"limitForBet"`
}
// NewRateLimiter is a constructor for NewRateLimiter.
func NewRateLimiter(config Config) *RateLimiter {
return &RateLimiter{
rateLimited: make(map[uuid.UUID]time.Time),
duration: config.LimitForBet,
}
}
// IsAllowed indicates if event is allowed to happen.
func (rateLimiter *RateLimiter) IsAllowed(key uuid.UUID, now time.Time) bool {
occursAt, exists := rateLimiter.rateLimited[key]
if exists {
return occursAt.Before(now)
}
return true
}
// SetLimit sets limit from the list of rate limited entities.
func (rateLimiter *RateLimiter) SetLimit(userID uuid.UUID) error {
rateLimiter.mu.Lock()
defer rateLimiter.mu.Unlock()
occursAt := time.Now().UTC().Add(rateLimiter.duration)
rateLimiter.rateLimited[userID] = occursAt
return nil
}
// GetDuration gets limit duration.
func (rateLimiter *RateLimiter) GetDuration() time.Duration {
return rateLimiter.duration
}