-
Notifications
You must be signed in to change notification settings - Fork 304
feat(webhook): add rate limiting to webhook endpoint #1210
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
Open
cjcocokrisp
wants to merge
8
commits into
argoproj-labs:master
Choose a base branch
from
cjcocokrisp:feat/webhook-extras
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2b8e464
feat(webhook): add rate limiting to the webhook endpoint
cjcocokrisp a1fdd60
feat(webhook): add env variables for rate limit flags and update mani…
cjcocokrisp 9bdd578
docs(webhook): add related docs for rate limiting
cjcocokrisp ae6b4a1
manifests(webhook): add optional to ratelimit env variables in deploy…
cjcocokrisp 155d8fe
fix: fix typo
cjcocokrisp 41e493b
fix(webhook): made adjustments based on review
cjcocokrisp 56b3178
fix(webhook): fix other typos
cjcocokrisp 678f7ec
feat(webhook): switch from custom implemented webhook over to uber ra…
cjcocokrisp 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
package webhook | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// RateLimiter implements a sliding window rate limiting algorithm | ||
type RateLimiter struct { | ||
mu sync.Mutex | ||
clients map[string][]int64 | ||
lastSeen map[string]time.Time | ||
window time.Duration | ||
allowed int | ||
done chan bool | ||
} | ||
cjcocokrisp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func NewRateLimiter(numRequets int, window time.Duration, cleanUpInterval time.Duration) *RateLimiter { | ||
cjcocokrisp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
limiter := RateLimiter{ | ||
clients: make(map[string][]int64), | ||
lastSeen: make(map[string]time.Time), | ||
window: window, | ||
allowed: numRequets, | ||
done: make(chan bool), | ||
} | ||
go limiter.StartCleanUp(cleanUpInterval) | ||
return &limiter | ||
} | ||
|
||
// Checks to see if a client has gone over the limit | ||
func (rl *RateLimiter) Allow(clientIP string) bool { | ||
now := time.Now() | ||
rl.mu.Lock() | ||
defer rl.mu.Unlock() | ||
|
||
if _, ok := rl.clients[clientIP]; !ok { | ||
rl.clients[clientIP] = []int64{} | ||
} | ||
|
||
allow := false | ||
windowStart := now.Unix() - int64(rl.window.Seconds()) | ||
filtered := []int64{} | ||
for _, ts := range rl.clients[clientIP] { | ||
if ts > windowStart { | ||
filtered = append(filtered, ts) | ||
cjcocokrisp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
rl.clients[clientIP] = filtered | ||
|
||
if len(filtered) < rl.allowed { | ||
rl.clients[clientIP] = append(filtered, now.Unix()) | ||
rl.lastSeen[clientIP] = now | ||
allow = true | ||
} | ||
|
||
return allow | ||
} | ||
|
||
// Cleans up the clients map at an interval to prevent stale clients from taking up memory | ||
func (rl *RateLimiter) StartCleanUp(interval time.Duration) { | ||
ticker := time.NewTicker(interval) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-rl.done: | ||
return | ||
case <-ticker.C: | ||
rl.CleanUp() | ||
} | ||
} | ||
} | ||
|
||
// Cleans up any clients that have not made a request in an amount of time over the window | ||
func (rl *RateLimiter) CleanUp() { | ||
rl.mu.Lock() | ||
defer rl.mu.Unlock() | ||
|
||
for k, v := range rl.lastSeen { | ||
now := time.Now() | ||
if v.Add(rl.window).Before(now) { | ||
delete(rl.clients, k) | ||
delete(rl.lastSeen, k) | ||
} | ||
} | ||
} | ||
|
||
// Stop the clean up goroutine | ||
func (rl *RateLimiter) StopCleanUp() { | ||
rl.done <- true | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.