-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
🩹 Fix limiter middleware db connection #1813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fdfc8da
remove release() method
kwarabei b410c27
Revert "remove release() method"
kwarabei 4c1ea42
Rearrange mutexes in limiter_fixed.go
kwarabei c01ed47
Merge branch 'master' into fix/limiterDBConn
kwarabei 1f2eab4
refactor limiter_fixed.go
kwarabei 0d36570
Update limiter_fixed.go
ReneWerner87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,9 @@ func (FixedWindow) New(cfg Config) fiber.Handler { | |
|
||
// Lock entry | ||
mux.Lock() | ||
defer func() { | ||
mux.Unlock() | ||
}() | ||
|
||
// Get entry from pool and release when finished | ||
e := manager.get(key) | ||
|
@@ -69,12 +72,6 @@ func (FixedWindow) New(cfg Config) fiber.Handler { | |
// Set how many hits we have left | ||
remaining := cfg.Max - e.currHits | ||
|
||
// Update storage | ||
manager.set(key, e, cfg.Expiration) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. think moving the set function must also be done in the part where the limit was reached |
||
|
||
// Unlock entry | ||
mux.Unlock() | ||
|
||
// Check if hits exceed the cfg.Max | ||
if remaining < 0 { | ||
// Return response with Retry-After header | ||
|
@@ -92,17 +89,18 @@ func (FixedWindow) New(cfg Config) fiber.Handler { | |
// Check for SkipFailedRequests and SkipSuccessfulRequests | ||
if (cfg.SkipSuccessfulRequests && c.Response().StatusCode() < fiber.StatusBadRequest) || | ||
(cfg.SkipFailedRequests && c.Response().StatusCode() >= fiber.StatusBadRequest) { | ||
mux.Lock() | ||
e.currHits-- | ||
remaining++ | ||
mux.Unlock() | ||
} | ||
|
||
// We can continue, update RateLimit headers | ||
c.Set(xRateLimitLimit, max) | ||
c.Set(xRateLimitRemaining, strconv.Itoa(remaining)) | ||
c.Set(xRateLimitReset, strconv.FormatUint(resetInSec, 10)) | ||
|
||
// Update storage | ||
manager.set(key, e, cfg.Expiration) | ||
|
||
return err | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not a good idea, the mutex in the defer would block the reading for too long
Because with c.Next() all possible handlers are executed