Skip to content

Commit

Permalink
Added doc strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebulizer1213 authored and Raven0213 committed Jul 27, 2022
1 parent d7398b8 commit 4f844e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 7 additions & 1 deletion gin_rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import (
)

type Store interface {
// Limit takes in a key and should return whether that key is allowed to make another request
Limit(key string) (bool, time.Duration)
// Skip takes in a *gin.Context and should return whether the rate limiting should be skipped for this request
Skip(c *gin.Context) bool
}

func RateLimiter(keyFunc func(c *gin.Context) string, errorHandler func(c *gin.Context, remaining time.Duration), s Store) func(ctx *gin.Context) {
// RateLimiter is a function to get gin.HandlerFunc
// keyFunc: takes in *gin.Context and return a string
// errorHandler: takes in *gin.Context and time.Duration
// store: Store
func RateLimiter(keyFunc func(c *gin.Context) string, errorHandler func(c *gin.Context, remaining time.Duration), s Store) gin.HandlerFunc {
return func(c *gin.Context) {
if s.Skip(c) {
c.Next()
Expand Down
7 changes: 5 additions & 2 deletions in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ func (s *inMemoryStoreType) Skip(c *gin.Context) bool {
}

type InMemoryOptions struct {
Rate time.Duration
// the user can make Limit amount of requests every Rate
Rate time.Duration
// the amount of requests that can be made every Rate
Limit uint
Skip func(c *gin.Context) bool
// takes in a *gin.Context and should return whether the rate limiting should be skipped for this request
Skip func(c *gin.Context) bool
}

func InMemoryStore(options *InMemoryOptions) Store {
Expand Down
12 changes: 8 additions & 4 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ func (s *redisStoreType) Skip(c *gin.Context) bool {
}

type RedisOptions struct {
Rate time.Duration
Limit uint
RedisClient redis.UniversalClient
// the user can make Limit amount of requests every Rate
Rate time.Duration
// the amount of requests that can be made every Rate
Limit uint
// takes in a *gin.Context and should return whether the rate limiting should be skipped for this request
Skip func(c *gin.Context) bool
PanicOnErr bool
RedisClient redis.UniversalClient
// should gin-rate-limit panic when there is an error with redis
PanicOnErr bool
}

func RedisStore(options *RedisOptions) Store {
Expand Down

0 comments on commit 4f844e8

Please sign in to comment.