-
Notifications
You must be signed in to change notification settings - Fork 0
Add global Cloudflare throttle for player_crawl; cap player_crawl workers at 50 #37
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5a10975
Initial plan
Copilot 647b084
Add global Cloudflare throttle for player_crawl and reduce Zeus WWW t…
Copilot a2a5184
Apply review feedback: throttle threshold, revert Zeus, cap workers a…
Copilot 3bfd3a8
Fix throttle gen counter: early return when already throttled
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package bungie | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "raidhub/lib/utils/logging" | ||
| ) | ||
|
|
||
| const ( | ||
| // cloudflareThrottleDuration is how long all workers pause after the throttle is activated. | ||
| cloudflareThrottleDuration = 60 * time.Second | ||
|
|
||
| // cloudflareThrottleWindow is the sliding window over which errors are counted. | ||
| cloudflareThrottleWindow = 10 * time.Second | ||
|
|
||
| // cloudflareThrottleMinErrors is the minimum number of Cloudflare errors within | ||
| // cloudflareThrottleWindow required to activate the global throttle. | ||
| cloudflareThrottleMinErrors = 3 | ||
| ) | ||
|
|
||
| var ( | ||
| cloudflareThrottleMu sync.Mutex | ||
| cloudflareThrottleCh chan struct{} // closed when NOT throttled; open (blocks) when throttled | ||
| cloudflareIsThrottled bool | ||
| cloudflareGeneration int // incremented on each new throttle activation; guards against stale timer callbacks | ||
| cloudflareErrorTimes []time.Time // sliding window of recent Cloudflare error timestamps | ||
| cloudflareThrottleLog = logging.NewLogger("CLOUDFLARE_THROTTLE") | ||
| ) | ||
|
|
||
| func init() { | ||
| // Start in unthrottled state: a closed channel unblocks all Select receivers immediately. | ||
| cloudflareThrottleCh = make(chan struct{}) | ||
| close(cloudflareThrottleCh) | ||
| } | ||
|
|
||
| // SignalCloudflareThrottle records a Cloudflare error and activates a global pause when | ||
| // cloudflareThrottleMinErrors errors occur within cloudflareThrottleWindow. | ||
| // All callers of WaitForCloudflareThrottle will block for cloudflareThrottleDuration. | ||
| // If the throttle is already active, this is a no-op. | ||
| func SignalCloudflareThrottle() { | ||
| cloudflareThrottleMu.Lock() | ||
| defer cloudflareThrottleMu.Unlock() | ||
|
|
||
| now := time.Now() | ||
|
|
||
| // Append the current error and prune events outside the sliding window. | ||
| cloudflareErrorTimes = append(cloudflareErrorTimes, now) | ||
| cutoff := now.Add(-cloudflareThrottleWindow) | ||
| start := 0 | ||
| for start < len(cloudflareErrorTimes) && cloudflareErrorTimes[start].Before(cutoff) { | ||
| start++ | ||
| } | ||
| cloudflareErrorTimes = cloudflareErrorTimes[start:] | ||
|
|
||
| // Only activate the throttle once the threshold is reached. | ||
| if len(cloudflareErrorTimes) < cloudflareThrottleMinErrors { | ||
| return | ||
| } | ||
|
|
||
| // If already throttled, nothing to do — the existing timer will clear it. | ||
| if cloudflareIsThrottled { | ||
| return | ||
| } | ||
|
|
||
| cloudflareGeneration++ | ||
| gen := cloudflareGeneration | ||
|
|
||
| // Replace the closed channel with a new open channel so waiters will block. | ||
| cloudflareIsThrottled = true | ||
| cloudflareThrottleCh = make(chan struct{}) | ||
| cloudflareThrottleLog.Warn("CLOUDFLARE_THROTTLE_ACTIVATED", nil, map[string]any{ | ||
| "duration_s": int(cloudflareThrottleDuration.Seconds()), | ||
| "error_count": len(cloudflareErrorTimes), | ||
| "window_s": int(cloudflareThrottleWindow.Seconds()), | ||
| }) | ||
|
|
||
| // Capture the current channel so the timer callback releases the right set of waiters. | ||
| // The generation counter guards against the unlikely race where this timer fires after | ||
| // the throttle has already been cleared and a new throttle period has started. | ||
| ch := cloudflareThrottleCh | ||
| time.AfterFunc(cloudflareThrottleDuration, func() { | ||
| cloudflareThrottleMu.Lock() | ||
| defer cloudflareThrottleMu.Unlock() | ||
| if cloudflareIsThrottled && cloudflareGeneration == gen { | ||
| cloudflareIsThrottled = false | ||
| close(ch) // Release all current waiters atomically | ||
| cloudflareThrottleLog.Info("CLOUDFLARE_THROTTLE_CLEARED", nil) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // WaitForCloudflareThrottle blocks until the global Cloudflare throttle is inactive or ctx is done. | ||
| // Returns ctx.Err() if the context is cancelled while waiting. | ||
| func WaitForCloudflareThrottle(ctx context.Context) error { | ||
| cloudflareThrottleMu.Lock() | ||
| ch := cloudflareThrottleCh | ||
| cloudflareThrottleMu.Unlock() | ||
|
|
||
| // If ch is closed (not throttled), this select arm returns immediately with no goroutine spawned. | ||
| select { | ||
| case <-ch: | ||
| return nil | ||
| case <-ctx.Done(): | ||
| return ctx.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.
But don't we increase the gen on every call? If 3 errors come in at the same time, wouldn't they all have different gens? We don't early return if throttling is enabled.
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.
Fixed in 3bfd3a8. Added an early return when
cloudflareIsThrottledis alreadytrue, sogenis only incremented and a timer is only spawned once per throttle activation — not on every subsequent error above the threshold. With the bug, 50 concurrent errors above the threshold would spawn 48 timers and increment gen 48 times; now only the first one to see!cloudflareIsThrottleddoes any work.