Commit 6515f22f authored by Robert Rudman's avatar Robert Rudman

Instead of catching json exceptions, check the response message for the backoff time.

parent 9ccde45f
Pipeline #42344047 passed with stages
in 4 minutes and 31 seconds
......@@ -28,6 +28,8 @@ namespace StackExchangeChat
public async Task<int> SendMessage(ChatSite chatSite, int roomId, string message)
{
var backoffRegex = new Regex(@"You can perform this action again in (\d+) seconds?");
return await ThrottlingUtils.Throttle(ChatThrottleGroups.WebRequestThrottle, async () =>
{
async Task<int> SendMessage()
......@@ -43,29 +45,24 @@ namespace StackExchangeChat
}));
var responseString = await response.Content.ReadAsStringAsync();
var responsePayload = JsonConvert.DeserializeObject<JObject>(responseString);
return int.Parse(responsePayload["id"].ToString());
}
var matchesBackoff = backoffRegex.Match(responseString);
if (matchesBackoff.Success)
{
var backoffSeconds = int.Parse(matchesBackoff.Groups[1].Value);
await Task.Delay(TimeSpan.FromSeconds(backoffSeconds + 5));
const int maxNumTries = 3;
var currentTryCount = 0;
var messageId = await SendMessage();
try
{
return await SendMessage();
}
catch (JsonReaderException)
{
// We failed to deserialize the response, we probably got throttled.
await Task.Delay(TimeSpan.FromSeconds(15));
await Task.Delay(TimeSpan.FromSeconds(15));
currentTryCount++;
if (currentTryCount >= maxNumTries)
throw;
return messageId;
}
return await SendMessage();
var responsePayload = JsonConvert.DeserializeObject<JObject>(responseString);
return int.Parse(responsePayload["id"].ToString());
}
return await SendMessage();
}, _ => Task.Delay(TimeSpan.FromSeconds(5)));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment