Commit 7f38f5aa authored by Robert Rudman's avatar Robert Rudman

If a chat message gets throttled, wait 15 seconds and try again, up to three times

parent 5f00452e
Pipeline #42343523 passed with stages
in 4 minutes and 46 seconds
......@@ -29,6 +29,8 @@ namespace StackExchangeChat
public async Task<int> SendMessage(ChatSite chatSite, int roomId, string message)
{
return await ThrottlingUtils.Throttle(ChatThrottleGroups.WebRequestThrottle, async () =>
{
async Task<int> SendMessage()
{
var fkey = (await _siteAuthenticator.GetRoomDetails(chatSite, roomId)).FKey;
await _siteAuthenticator.AuthenticateClient(_httpClient, chatSite);
......@@ -43,6 +45,26 @@ namespace StackExchangeChat
var responseString = await response.Content.ReadAsStringAsync();
var responsePayload = JsonConvert.DeserializeObject<JObject>(responseString);
return int.Parse(responsePayload["id"].ToString());
}
const int maxNumTries = 3;
var currentTryCount = 0;
try
{
return await SendMessage();
}
catch (Exception)
{
// We failed to deserialize the response, we probably got throttled.
await Task.Delay(TimeSpan.FromSeconds(15));
currentTryCount++;
if (currentTryCount >= maxNumTries)
throw;
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