-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathratelimit.go
57 lines (53 loc) · 1.52 KB
/
ratelimit.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
package main
import (
"fmt"
"net/http"
"sync"
"time"
)
const DATE_FORMAT = "2006-01-02T15:04:05"
type RateLimit struct {
mu sync.Mutex
hits map[string]uint64
limitExceeded bool
limitLifted time.Time
}
func (rl *RateLimit) ratelimit(w http.ResponseWriter, r *http.Request) {
if rl.limitExceeded && time.Now().Before(rl.limitLifted) {
w.WriteHeader(429)
w.Write([]byte("Rate Limited"))
return
}
if rl.limitExceeded && time.Now().After(rl.limitLifted) {
rl.limitExceeded = false
}
rl.mu.Lock()
timestamp := time.Now()
strTimestamp := timestamp.Format(DATE_FORMAT)
if val, ok := rl.hits[strTimestamp]; ok {
if val == 5 {
rl.limitExceeded = true
rl.limitLifted = time.Now().Add(time.Second * 10)
} else {
rl.hits[strTimestamp] = val + 1
}
} else {
rl.hits[strTimestamp] = 1
}
rl.mu.Unlock()
timestampOneSecondEarlier := time.Now().Add(time.Duration(-1) * time.Second)
if rl.hits[timestampOneSecondEarlier.Format(DATE_FORMAT)] == 5 {
w.Write([]byte(fmt.Sprintf("DONE! You did it! Hitting API at %d requests in a given second\n", rl.hits[timestampOneSecondEarlier.Format(DATE_FORMAT)])))
} else {
w.Write([]byte(fmt.Sprintf("Hitting API at %d requests in a given second\n", rl.hits[timestampOneSecondEarlier.Format(DATE_FORMAT)])))
}
if len(rl.hits) > 100000 {
rl.mu.Lock()
fmt.Printf("Map is getting big, resetting...\n")
oldVal := rl.hits[strTimestamp]
rl.hits = make(map[string]uint64)
rl.hits[strTimestamp] = oldVal
time.Sleep(1 * time.Second)
rl.mu.Unlock()
}
}