You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
returnError<T>(string.Format("You are sending too many requests and are being rate limited for %d seconds. Call was to endpoint %s",secondsLeftOfRateLimit,method));
160
+
}
161
+
}
162
+
163
+
164
+
165
+
#region Rate Limiting Support
166
+
167
+
publicclassRateLimiter
168
+
{
169
+
/* -- Configurable constants -- */
170
+
// Tripwire settings, allow for a max total of n requests per x seconds
171
+
protectedconstintTripWireTimeFrameSeconds=60;
172
+
protectedconstintMaxRequestsPerTripWireTimeFrame=280;// The backend rate limit is 300 requests per minute so we'll limit it slightly before that
173
+
protectedconstintSecondsPerBucket=5;// Needs to evenly divide the time frame
174
+
175
+
// Moving average settings, allow for a max average of n requests per x seconds
176
+
protectedconstfloatAllowXPercentOfTripWireMaxForMovingAverage=0.8f;// Moving average threshold (the average number of requests per bucket) is set slightly lower to stop constant abusive call behaviour just under the tripwire limit
177
+
protectedconstintCountMovingAverageAcrossNTripWireTimeFrames=3;// Count Moving average across a longer time period
_totalRequestsInBucketsInTripWireTimeFrame-=buckets[bucketMovingOutOfTripWireTimeFrame];// Remove the request count from the bucket that is moving out of the time frame from trip wire count
226
+
_totalRequestsInBuckets-=buckets[bucketIndex];// Remove the count from the bucket we're moving into from the total before emptying it
227
+
buckets[bucketIndex]=0;
228
+
}
229
+
230
+
return(lastBucket+moveOverXBuckets)%buckets.Length;// Step to next bucket and wrap around if necessary;
buckets[currentBucket]++;// Increment the current bucket
249
+
_totalRequestsInBuckets++;// Increment the total request count
250
+
_totalRequestsInBucketsInTripWireTimeFrame++;// Increment the request count for the current time frame
251
+
252
+
isRateLimited|=_totalRequestsInBucketsInTripWireTimeFrame>=MaxRequestsPerTripWireTimeFrame;// If the request count for the time frame is greater than the max requests per time frame, set isRateLimited to true
253
+
isRateLimited|=_totalRequestsInBuckets/RateLimitMovingAverageBucketCount>MaxRequestsPerBucketOnMovingAverage;// If the average number of requests per bucket is greater than the max requests on moving average, set isRateLimited to true
254
+
#if UNITY_EDITOR
255
+
if(_totalRequestsInBucketsInTripWireTimeFrame>=MaxRequestsPerTripWireTimeFrame)Debug.Log("Rate Limit Hit due to Trip Wire, count = "+_totalRequestsInBucketsInTripWireTimeFrame+" out of allowed "+MaxRequestsPerTripWireTimeFrame);
256
+
if(_totalRequestsInBuckets/RateLimitMovingAverageBucketCount>MaxRequestsPerBucketOnMovingAverage)Debug.Log("Rate Limit Hit due to Moving Average, count = "+_totalRequestsInBuckets/RateLimitMovingAverageBucketCount+" out of allowed "+MaxRequestsPerBucketOnMovingAverage);
0 commit comments