-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49995f7
commit 7748692
Showing
5 changed files
with
197 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package GinRateLimit | ||
|
||
import ( | ||
"context" | ||
"github.com/go-redis/redis/v8" | ||
"time" | ||
) | ||
|
||
type RedisStoreType struct { | ||
rate int64 | ||
limit int | ||
client *redis.Client | ||
ctx context.Context | ||
} | ||
|
||
func (s *RedisStoreType) Limit(key string) bool { | ||
p := s.client.Pipeline() | ||
defer func(s *RedisStoreType, p redis.Pipeliner) { | ||
p.Exec(s.ctx) | ||
p.Close() | ||
}(s, p) | ||
ts, err := s.client.Get(s.ctx, key+"ts").Int64() | ||
if err != nil { | ||
ts = time.Now().Unix() | ||
} | ||
hits, err := s.client.Get(s.ctx, key+"hits").Int64() | ||
if err != nil { | ||
hits = 0 | ||
} | ||
p.Expire(s.ctx, key+"hits", time.Duration(int64(time.Second)*s.rate*2)) | ||
p.Expire(s.ctx, key+"ts", time.Duration(int64(time.Second)*s.rate*2)) | ||
if ts+s.rate <= time.Now().Unix() { | ||
p.Set(s.ctx, key+"hits", 0, time.Duration(0)) | ||
} | ||
if hits >= int64(s.limit) { | ||
return true | ||
} | ||
p.Incr(s.ctx, key+"hits") | ||
p.Set(s.ctx, key+"ts", time.Now().Unix(), time.Duration(0)) | ||
return false | ||
} | ||
|
||
func RedisStore(rate time.Duration, limit int, redisClient *redis.Client) *RedisStoreType { | ||
return &RedisStoreType{client: redisClient, rate: int64(rate.Seconds()), limit: limit, ctx: context.TODO()} | ||
} |