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
......@@ -30,20 +30,42 @@ namespace StackExchangeChat
{
return await ThrottlingUtils.Throttle(ChatThrottleGroups.WebRequestThrottle, async () =>
{
var fkey = (await _siteAuthenticator.GetRoomDetails(chatSite, roomId)).FKey;
await _siteAuthenticator.AuthenticateClient(_httpClient, chatSite);
var response = await _httpClient.PostAsync($"https://{chatSite.ChatDomain}/chats/{roomId}/messages/new",
new FormUrlEncodedContent(
new Dictionary<string, string>
{
{"text", message},
{"fkey", fkey}
}));
async Task<int> SendMessage()
{
var fkey = (await _siteAuthenticator.GetRoomDetails(chatSite, roomId)).FKey;
await _siteAuthenticator.AuthenticateClient(_httpClient, chatSite);
var response = await _httpClient.PostAsync($"https://{chatSite.ChatDomain}/chats/{roomId}/messages/new",
new FormUrlEncodedContent(
new Dictionary<string, string>
{
{"text", message},
{"fkey", fkey}
}));
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();
}
var responseString = await response.Content.ReadAsStringAsync();
var responsePayload = JsonConvert.DeserializeObject<JObject>(responseString);
return int.Parse(responsePayload["id"].ToString());
}, _ => 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