Skip to content

Commit b5b2dbe

Browse files
feat: add support for custom throttling responses on rate limit (#347)
Co-authored-by: Gavin Barron <[email protected]>
1 parent 0571d4e commit b5b2dbe

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

m365-developer-proxy-plugins/Behavior/RateLimitingPlugin.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,21 @@ _urlsToWatch is null ||
244244
var headers = _configuration.CustomResponse.ResponseHeaders is not null ?
245245
_configuration.CustomResponse.ResponseHeaders.Select(h => new HttpHeader(h.Key, h.Value)) :
246246
Array.Empty<HttpHeader>();
247+
248+
// allow custom throttling response
249+
var responseCode = (HttpStatusCode)(_configuration.CustomResponse.ResponseCode ?? 200);
250+
if (responseCode == HttpStatusCode.TooManyRequests) {
251+
e.ThrottledRequests.Add(new ThrottlerInfo(
252+
BuildThrottleKey(request),
253+
ShouldThrottle,
254+
DateTime.Now.AddSeconds(_configuration.RetryAfterSeconds)
255+
));
256+
}
257+
247258
string body = _configuration.CustomResponse.ResponseBody is not null ?
248259
JsonSerializer.Serialize(_configuration.CustomResponse.ResponseBody, new JsonSerializerOptions { WriteIndented = true }) :
249260
"";
250-
e.Session.GenericResponse(body, (HttpStatusCode)(_configuration.CustomResponse.ResponseCode ?? 200), headers);
261+
e.Session.GenericResponse(body, responseCode, headers);
251262
state.HasBeenSet = true;
252263
}
253264
else

0 commit comments

Comments
 (0)