Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebulizer1213 committed Jan 22, 2022
1 parent 10bb2f2 commit 49995f7
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions GinRateLimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type user struct {
tokens int
}

func clearInBackground(data map[string]user, rate int, mutex *sync.Mutex) {
func clearInBackground(data map[string]*user, rate int, mutex *sync.Mutex) {
for {
mutex.Lock()
for k, v := range data {
Expand All @@ -27,7 +27,7 @@ func clearInBackground(data map[string]user, rate int, mutex *sync.Mutex) {
type InMemoryStoreType struct {
rate int
limit int
data map[string]user
data map[string]*user
mutex *sync.Mutex
}

Expand All @@ -36,7 +36,7 @@ func (s *InMemoryStoreType) Limit(key string) bool {
defer s.mutex.Unlock()
_, ok := s.data[key]
if !ok {
s.data[key] = user{int(time.Now().Unix()), s.limit}
s.data[key] = &user{int(time.Now().Unix()), s.limit}
}
u := s.data[key]
if u.ts+s.rate <= int(time.Now().Unix()) {
Expand All @@ -49,7 +49,6 @@ func (s *InMemoryStoreType) Limit(key string) bool {
u.ts = int(time.Now().Unix())
s.data[key] = u
return false

}

type store interface {
Expand All @@ -58,7 +57,7 @@ type store interface {

func InMemoryStore(rate int, limit int) *InMemoryStoreType {
mutex := &sync.Mutex{}
data := map[string]user{}
data := map[string]*user{}
store := InMemoryStoreType{rate, limit, data, mutex}
go clearInBackground(data, rate, mutex)
return &store
Expand Down

0 comments on commit 49995f7

Please sign in to comment.