Skip to content

Commit

Permalink
Merge pull request #5 from sercanarga/master
Browse files Browse the repository at this point in the history
Rate-Limit header info updated according to standards
  • Loading branch information
Nebulizer1213 authored Feb 22, 2023
2 parents c0be127 + b089895 commit 6b2e0e8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,14 @@ type CustomStore struct {
func (s *CustomStore) Limit(key string, c *gin.Context) Info {
if UserWentOverLimit {
return Info{
Limit: 100,
RateLimited: true,
ResetTime: reset,
RemainingHits: 0,
}
}
return Info{
Limit: 100,
RateLimited: false,
ResetTime: reset,
RemainingHits: remaining,
Expand Down
7 changes: 5 additions & 2 deletions gin_rate_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type Info struct {
Limit uint
RateLimited bool
ResetTime time.Time
RemainingHits uint
Expand All @@ -31,14 +32,16 @@ func RateLimiter(s Store, options *Options) gin.HandlerFunc {
}
if options.ErrorHandler == nil {
options.ErrorHandler = func(c *gin.Context, info Info) {
c.Header("X-Rate-Limit-Reset", fmt.Sprintf("%.2f", time.Until(info.ResetTime).Seconds()))
c.Header("X-Rate-Limit-Limit", fmt.Sprintf("%d", info.Limit))
c.Header("X-Rate-Limit-Reset", fmt.Sprintf("%d", info.ResetTime.Unix()))
c.String(429, "Too many requests")
}
}
if options.BeforeResponse == nil {
options.BeforeResponse = func(c *gin.Context, info Info) {
c.Header("X-Rate-Limit-Limit", fmt.Sprintf("%d", info.Limit))
c.Header("X-Rate-Limit-Remaining", fmt.Sprintf("%v", info.RemainingHits))
c.Header("X-Rate-Limit-Reset", fmt.Sprintf("%.2f", time.Until(info.ResetTime).Seconds()))
c.Header("X-Rate-Limit-Reset", fmt.Sprintf("%d", info.ResetTime.Unix()))
}
}
if options.KeyFunc == nil {
Expand Down
3 changes: 3 additions & 0 deletions in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ func (s *inMemoryStoreType) Limit(key string, c *gin.Context) Info {
}
if s.skip != nil && s.skip(c) {
return Info{
Limit: s.limit,
RateLimited: false,
ResetTime: time.Now().Add(time.Duration((s.rate - (time.Now().Unix() - u.ts)) * time.Second.Nanoseconds())),
RemainingHits: u.tokens,
}
}
if u.tokens <= 0 {
return Info{
Limit: s.limit,
RateLimited: true,
ResetTime: time.Now().Add(time.Duration((s.rate - (time.Now().Unix() - u.ts)) * time.Second.Nanoseconds())),
RemainingHits: 0,
Expand All @@ -59,6 +61,7 @@ func (s *inMemoryStoreType) Limit(key string, c *gin.Context) Info {
u.ts = time.Now().Unix()
s.data.Store(key, u)
return Info{
Limit: s.limit,
RateLimited: false,
ResetTime: time.Now().Add(time.Duration((s.rate - (time.Now().Unix() - u.ts)) * time.Second.Nanoseconds())),
RemainingHits: u.tokens,
Expand Down
5 changes: 5 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (s *redisStoreType) Limit(key string, c *gin.Context) Info {
}
if s.skip != nil && s.skip(c) {
return Info{
Limit: s.limit,
RateLimited: false,
ResetTime: time.Now().Add(time.Duration((s.rate - (time.Now().Unix() - ts)) * time.Second.Nanoseconds())),
RemainingHits: s.limit - uint(hits),
Expand All @@ -50,13 +51,15 @@ func (s *redisStoreType) Limit(key string, c *gin.Context) Info {
panic(err)
} else {
return Info{
Limit: s.limit,
RateLimited: false,
ResetTime: time.Now().Add(time.Duration((s.rate - (time.Now().Unix() - ts)) * time.Second.Nanoseconds())),
RemainingHits: 0,
}
}
}
return Info{
Limit: s.limit,
RateLimited: true,
ResetTime: time.Now().Add(time.Duration((s.rate - (time.Now().Unix() - ts)) * time.Second.Nanoseconds())),
RemainingHits: 0,
Expand All @@ -74,13 +77,15 @@ func (s *redisStoreType) Limit(key string, c *gin.Context) Info {
panic(err)
} else {
return Info{
Limit: s.limit,
RateLimited: false,
ResetTime: time.Now().Add(time.Duration((s.rate - (time.Now().Unix() - ts)) * time.Second.Nanoseconds())),
RemainingHits: s.limit - uint(hits),
}
}
}
return Info{
Limit: s.limit,
RateLimited: false,
ResetTime: time.Now().Add(time.Duration((s.rate - (time.Now().Unix() - ts)) * time.Second.Nanoseconds())),
RemainingHits: s.limit - uint(hits),
Expand Down

0 comments on commit 6b2e0e8

Please sign in to comment.