Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/SnackFlow.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"DefaultConnection": "Host=localhost;Database=snackflow_db;Username=postgres;Password=root;Port=5432"
},
"AWS": {
"Region": "us-east-1"
"Region": "us-east-1",
"SES": {
"MaxConcurrentEmails": 3
}
},
"AccessTokenSettings": {
"Key": "Common/Certificates/access-token-jwt-key.pfx",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace SnackFlow.Application.Abstractions.Services.Concurrency;

/// <summary>
/// Fornece funcionalidade para gerenciar concorrência durante operações relacionadas a emails.
/// </summary>
public interface IEmailConcurrencyService
{
/// <summary>
/// Obtém um bloqueio de concorrência para gerenciar o acesso durante operações relacionadas a e-mails.
/// </summary>
/// <param name="cancellationToken">
/// Um token para monitorar solicitações de cancelamento.
/// </param>
/// <returns>
/// Uma tarefa que representa uma operação assíncrona. O resultado contém um objeto descartável
/// que deve ser descartado para liberar o bloqueio.
/// </returns>
Task<IDisposable> AcquireAsync(CancellationToken cancellationToken = default);
}
6 changes: 6 additions & 0 deletions src/SnackFlow.Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
using Serilog.Events;
using SnackFlow.Application.Abstractions.Schedulers;
using SnackFlow.Application.Abstractions.Services;
using SnackFlow.Application.Abstractions.Services.Concurrency;
using SnackFlow.Domain.Repositories;
using SnackFlow.Infrastructure.Jobs;
using SnackFlow.Infrastructure.Schedulers;
using SnackFlow.Infrastructure.Services;
using SnackFlow.Infrastructure.Services.Concurrency;
using SnackFlow.Infrastructure.Services.EmailTemplateService;

namespace SnackFlow.Infrastructure;
Expand Down Expand Up @@ -92,6 +94,7 @@ private static void AddRepositories(this IServiceCollection services)

private static void AddServices(this IServiceCollection services)
{
// Transient
services.AddTransient<ICertificateService, CertificateService>();
services.AddTransient<IEmailTemplateService, EmailTemplateService>();
services.AddTransient<IEmailService, EmailService>();
Expand All @@ -100,6 +103,9 @@ private static void AddServices(this IServiceCollection services)
services.AddTransient<ITokenService, JwtTokenService>();
services.AddTransient<IUserCreationService, UserCreationService>();
services.AddTransient<ICurrentUserService, CurrentUserService>();

// Singleton
services.AddSingleton<IEmailConcurrencyService, EmailConcurrencyService>();
}

private static void AddAwsServices(this IServiceCollection services, IConfiguration configuration)
Expand Down
3 changes: 3 additions & 0 deletions src/SnackFlow.Infrastructure/Jobs/SendingEmailJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
using Newtonsoft.Json.Linq;
using Quartz;
using SnackFlow.Application.Abstractions.Services;
using SnackFlow.Application.Abstractions.Services.Concurrency;
using SnackFlow.Domain.Constants;

namespace SnackFlow.Infrastructure.Jobs;

public sealed class SendingEmailJob(
IEmailService emailService,
IEmailTemplateService templateEmailService,
IEmailConcurrencyService emailConcurrencyService,
ILogger<SendingEmailJob> logger)
: IJob
{
public async Task Execute(IJobExecutionContext context)
{
using var semaphoreRelease = await emailConcurrencyService.AcquireAsync();
var jobDataMap = context.Trigger.JobDataMap;
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.Configuration;
using SnackFlow.Application.Abstractions.Services.Concurrency;

namespace SnackFlow.Infrastructure.Services.Concurrency;

public sealed class EmailConcurrencyService
: IEmailConcurrencyService
{
private readonly SemaphoreSlim _semaphoreSlim;

public EmailConcurrencyService(IConfiguration configuration)
{
var maxConcurrent = configuration.GetValue("AWS:SES:MaxConcurrentEmails", 3);
_semaphoreSlim = new SemaphoreSlim(maxConcurrent, maxConcurrent);
}

public async Task<IDisposable> AcquireAsync(CancellationToken cancellationToken = default)
{
await _semaphoreSlim.WaitAsync(cancellationToken);
return new SemaphoreReleaser(_semaphoreSlim);
}

private sealed class SemaphoreReleaser(SemaphoreSlim semaphoreSlim) : IDisposable
{
public void Dispose() => semaphoreSlim.Dispose();
}
}