Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebulizer1213 committed Dec 21, 2021
1 parent 9938c7d commit bb2f5ac
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions GinRateLimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
)

type user struct {
time int
requests int
ts int
tokens int
}

func clearInBackground(data map[string]user, rate int, mutex *sync.Mutex) {
for {
mutex.Lock()
for k, v := range data {
if v.time+rate <= int(time.Now().Unix()) {
if v.ts+rate <= int(time.Now().Unix()) {
delete(data, k)
}
}
Expand All @@ -39,14 +39,14 @@ func (s *InMemoryStoreType) Limit(key string) bool {
s.data[key] = user{int(time.Now().Unix()), s.limit}
}
u := s.data[key]
if u.time+s.rate <= int(time.Now().Unix()) {
u.requests = s.limit
if u.ts+s.rate <= int(time.Now().Unix()) {
u.tokens = s.limit
}
if u.requests <= 0 {
if u.tokens <= 0 {
return true
}
u.requests--
u.time = int(time.Now().Unix())
u.tokens--
u.ts = int(time.Now().Unix())
s.data[key] = u
return false

Expand Down

0 comments on commit bb2f5ac

Please sign in to comment.