From 01f0776a340639ba7c6c35926335f53945e88784 Mon Sep 17 00:00:00 2001 From: Ronny Gunawan <3048897+ronnygunawan@users.noreply.github.com> Date: Sun, 3 Dec 2023 23:13:40 +0700 Subject: [PATCH] Add global rate limit for image generation --- BotNet.Services/BotCommands/OpenAI.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/BotNet.Services/BotCommands/OpenAI.cs b/BotNet.Services/BotCommands/OpenAI.cs index a427cf0..24507e4 100644 --- a/BotNet.Services/BotCommands/OpenAI.cs +++ b/BotNet.Services/BotCommands/OpenAI.cs @@ -767,7 +767,8 @@ await botClient.SendTextMessageAsync( } private static readonly RateLimiter IMAGE_GENERATION_PER_USER_RATE_LIMITER = RateLimiter.PerUser(1, TimeSpan.FromMinutes(10)); - private static readonly RateLimiter IMAGE_GENERATION_PER_CHAT_RATE_LIMITER = RateLimiter.PerUser(2, TimeSpan.FromMinutes(3)); + private static readonly RateLimiter IMAGE_GENERATION_PER_CHAT_RATE_LIMITER = RateLimiter.PerChat(2, TimeSpan.FromMinutes(5)); + private static readonly RateLimiter IMAGE_GENERATION_GLOBAL_RATE_LIMITER = RateLimiter.PerChat(1, TimeSpan.FromMinutes(1)); public static async Task StreamChatWithFriendlyBotAsync( ITelegramBotClient botClient, IServiceProvider serviceProvider, @@ -839,6 +840,10 @@ await serviceProvider.GetRequiredService().StreamChatAsync( chatId: message.Chat.Id, userId: message.From.Id ); + IMAGE_GENERATION_GLOBAL_RATE_LIMITER.ValidateActionRate( + chatId: 0, + userId: 0 + ); Message busyMessage = await botClient.SendTextMessageAsync( chatId: message.Chat.Id, text: "Generating image… ⏳",