forked from AcaMate/AcaMate_API
[♻️] PUSH API 리팩토링 진행 중_4 : 대용량 발신 위한 버전 작성 중
This commit is contained in:
parent
242f1a48df
commit
9413dbbea8
|
@ -12,13 +12,10 @@ namespace AcaMate.V1.Controllers;
|
|||
[ApiExplorerSettings(GroupName = "공통")]
|
||||
public class PushController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly IWebHostEnvironment _env;
|
||||
private readonly ILogger<PushController> _logger;
|
||||
private readonly IPushQueue _pushQueue;
|
||||
public PushController(IWebHostEnvironment env, ILogger<PushController> logger, IPushQueue pushQueue)
|
||||
public PushController(ILogger<PushController> logger, IPushQueue pushQueue)
|
||||
{
|
||||
_env = env;
|
||||
_logger = logger;
|
||||
_pushQueue = pushQueue;
|
||||
}
|
||||
|
|
63
Program/V1/Services/InMemoryPushQueue.cs
Normal file
63
Program/V1/Services/InMemoryPushQueue.cs
Normal file
|
@ -0,0 +1,63 @@
|
|||
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AcaMate.V1.Models;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace AcaMate.V1.Services;
|
||||
|
||||
public interface IPushQueue
|
||||
{
|
||||
void Enqueue(PushRequest request);
|
||||
Task<PushRequest> DequeueAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public class InMemoryPushQueue: IPushQueue
|
||||
{
|
||||
private readonly ConcurrentQueue<PushRequest> _queue = new ConcurrentQueue<PushRequest>();
|
||||
private readonly SemaphoreSlim _signal = new SemaphoreSlim(0);
|
||||
|
||||
public void Enqueue(PushRequest request)
|
||||
{
|
||||
if( request is null )
|
||||
throw new ArgumentNullException(nameof(request));
|
||||
_queue.Enqueue(request);
|
||||
_signal.Release();
|
||||
}
|
||||
|
||||
public async Task<PushRequest> DequeueAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _signal.WaitAsync(cancellationToken);
|
||||
_queue.TryDequeue(out var request);
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
public class PushBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly IPushQueue _queue;
|
||||
private readonly IApnsPushService _pushService;
|
||||
|
||||
public PushBackgroundService(IPushQueue queue, IApnsPushService pushService)
|
||||
{
|
||||
_queue = queue;
|
||||
_pushService = pushService;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var pushRequest = await _queue.DequeueAsync(stoppingToken);
|
||||
try
|
||||
{
|
||||
await _pushService.SendPushNotificationAsync(pushRequest.deviceToken, pushRequest.payload);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"푸시 전송 실패: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user