From 655e00597bf92695c87214a665a186563de72619 Mon Sep 17 00:00:00 2001 From: Yuvaraj Date: Wed, 18 Sep 2024 11:42:35 -0700 Subject: [PATCH] Limit 14 calls max per second --- NotificationSystem/NotificationSystem.csproj | 1 + NotificationSystem/SendModeratorEmail.cs | 6 +++++- NotificationSystem/SendSubscriberEmail.cs | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NotificationSystem/NotificationSystem.csproj b/NotificationSystem/NotificationSystem.csproj index c6048b1..387e0fb 100644 --- a/NotificationSystem/NotificationSystem.csproj +++ b/NotificationSystem/NotificationSystem.csproj @@ -9,6 +9,7 @@ + diff --git a/NotificationSystem/SendModeratorEmail.cs b/NotificationSystem/SendModeratorEmail.cs index 12cf196..22eb958 100644 --- a/NotificationSystem/SendModeratorEmail.cs +++ b/NotificationSystem/SendModeratorEmail.cs @@ -9,6 +9,8 @@ using NotificationSystem.Utilities; using Amazon; using Amazon.SimpleEmail; +using RateLimiter; +using ComposableAsync; namespace NotificationSystem { @@ -57,11 +59,13 @@ public static async Task Run( // TODO: make better email string body = EmailTemplate.GetModeratorEmailBody(documentTimeStamp, location); + var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(14, TimeSpan.FromSeconds(1)); var aws = new AmazonSimpleEmailServiceClient(RegionEndpoint.USWest2); log.LogInformation("Retrieving email list and sending notifications"); foreach (var emailEntity in EmailHelpers.GetEmailEntities(cloudTable, "Moderator")) { - string emailSubject = string.Format("OrcaHello Candidate at location {0}", location); + await timeConstraint; + string emailSubject = string.Format("OrcaHello Candidate at location {0}", location); var email = EmailHelpers.CreateEmail(Environment.GetEnvironmentVariable("SenderEmail"), emailEntity.Email, emailSubject, body); await aws.SendEmailAsync(email); diff --git a/NotificationSystem/SendSubscriberEmail.cs b/NotificationSystem/SendSubscriberEmail.cs index 6370ff2..768519b 100644 --- a/NotificationSystem/SendSubscriberEmail.cs +++ b/NotificationSystem/SendSubscriberEmail.cs @@ -12,6 +12,8 @@ using NotificationSystem.Utilities; using Amazon; using Amazon.SimpleEmail; +using RateLimiter; +using ComposableAsync; namespace NotificationSystem { @@ -38,10 +40,12 @@ public static async Task Run( log.LogInformation("Creating email message"); var body = await CreateBody(cloudQueue); + var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(14, TimeSpan.FromSeconds(1)); var aws = new AmazonSimpleEmailServiceClient(RegionEndpoint.USWest2); log.LogInformation("Retrieving email list and sending notifications"); foreach (var emailEntity in EmailHelpers.GetEmailEntities(cloudTable, "Subscriber")) { + await timeConstraint; var email = EmailHelpers.CreateEmail(Environment.GetEnvironmentVariable("SenderEmail"), emailEntity.Email, "Notification: Orca detected!", body); await aws.SendEmailAsync(email);