From e8b2c0dc6029717d7c1844daa54fa24fc075278c Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Thu, 5 Nov 2020 19:23:42 +0100 Subject: [PATCH] Fix #4 Crash when hitting rate-limit --- Commands.cs | 44 ++++++++++++++++++++++++++++++++------------ RSSBot.csproj | 8 ++++---- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/Commands.cs b/Commands.cs index d355c60..56c8788 100644 --- a/Commands.cs +++ b/Commands.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Text; using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; using System.Web; using CodeHollow.FeedReader; using NLog; @@ -244,18 +247,9 @@ namespace RSSBot { // Send foreach (long chatId in feed.Subs.ToList()) - try { - await Bot.BotClient.SendTextMessageAsync(chatId, text, ParseMode.Html, true, true); - } catch (ApiRequestException e) { - if (e.ErrorCode.Equals(403)) { - Logger.Warn(e.Message); - feed.Cleanup(chatId); - if (feed.Subs.Count == 0) // was last subscriber - Bot.RssBotFeeds.Remove(feed); - } else { - Logger.Error($"{e.ErrorCode}: {e.Message}"); - } - } + { + await SendFinishedMessage(chatId, text, feed); + } } } @@ -266,6 +260,32 @@ namespace RSSBot { Bot.JobQueue.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMilliseconds(-1)); } + private static async Task SendFinishedMessage(long chatId, string text, RssBotFeed feed, int waitTime = 10) + { + try + { + await Bot.BotClient.SendTextMessageAsync(chatId, text, ParseMode.Html, true, true); + Thread.Sleep(1000); + } catch (ApiRequestException e) + { + if (e.ErrorCode.Equals(403)) + { + Logger.Warn(e.Message); + feed.Cleanup(chatId); + if (feed.Subs.Count == 0) // was last subscriber + Bot.RssBotFeeds.Remove(feed); + } else + { + Logger.Error($"{e.ErrorCode}: {e.Message}"); + } + } catch (HttpRequestException e) // likely 429 + { + Logger.Warn($"Got rate limited, waiting {waitTime} seconds..."); + Thread.Sleep(waitTime * 1000); + await SendFinishedMessage(chatId, text, feed, (waitTime * 2)); + } + } + public static async void ShowAvailableFeeds(Message message, GroupCollection args) { string url = args[1].Value; IEnumerable feeds; diff --git a/RSSBot.csproj b/RSSBot.csproj index 0b71896..d685d91 100644 --- a/RSSBot.csproj +++ b/RSSBot.csproj @@ -4,7 +4,7 @@ Exe netcoreapp3.1 win-x64;osx-x64;linux-x64;linux-arm - 1.0.1 + 1.1.0 false @@ -17,9 +17,9 @@ - - - + + +