Skip to content

Commit

Permalink
Limit 14 calls max per second
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuvaraj committed Sep 18, 2024
1 parent cb722e1 commit 655e005
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions NotificationSystem/NotificationSystem.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.10" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.5" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
<PackageReference Include="RateLimiter" Version="2.2.0" />
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.33" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
Expand Down
6 changes: 5 additions & 1 deletion NotificationSystem/SendModeratorEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using NotificationSystem.Utilities;
using Amazon;
using Amazon.SimpleEmail;
using RateLimiter;
using ComposableAsync;

namespace NotificationSystem
{
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions NotificationSystem/SendSubscriberEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using NotificationSystem.Utilities;
using Amazon;
using Amazon.SimpleEmail;
using RateLimiter;
using ComposableAsync;

namespace NotificationSystem
{
Expand All @@ -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);
Expand Down

0 comments on commit 655e005

Please sign in to comment.