diff --git a/src/FakeRelay.Web/Controllers/AcitivityPubController.cs b/src/FakeRelay.Web/Controllers/AcitivityPubController.cs index 6e02867..42a8196 100644 --- a/src/FakeRelay.Web/Controllers/AcitivityPubController.cs +++ b/src/FakeRelay.Web/Controllers/AcitivityPubController.cs @@ -1,7 +1,5 @@ -using System.Threading.Tasks; using FakeRelay.Core; using FakeRelay.Core.Helpers; -using FakeRelay.Web.Services; using Microsoft.AspNetCore.Mvc; namespace FakeRelay.Web.Controllers; @@ -10,15 +8,15 @@ public class AcitivityPubController : Controller { [Route("actor")] - public async Task Actor([FromServices] IBackgroundTaskQueue taskQueue) => + public async Task Actor() => Content(MastodonHelper.GetActorStaticContent(), "application/activity+json; charset=utf-8"); [Route("inbox"), HttpPost] - public async Task Inbox([FromBody] ActivityPubModel model, [FromServices] IBackgroundTaskQueue taskQueue) + public async Task Inbox([FromBody] ActivityPubModel model) { if (model.Type == "Follow") { - await taskQueue.QueueBackgroundWorkItemAsync(_ => MastodonHelper.ProcessInstanceFollowAsync(model)); + await MastodonHelper.ProcessInstanceFollowAsync(model); } return Content("{}", "application/activity+json"); diff --git a/src/FakeRelay.Web/Program.cs b/src/FakeRelay.Web/Program.cs index 47747a3..0e0cbe8 100644 --- a/src/FakeRelay.Web/Program.cs +++ b/src/FakeRelay.Web/Program.cs @@ -1,5 +1,4 @@ using FakeRelay.Core; -using FakeRelay.Web.Services; using Microsoft.AspNetCore.HttpOverrides; using Prometheus; @@ -26,9 +25,6 @@ builder.Services.Configure(options => options.ForwardLimit = 1; }); -builder.Services.AddHostedService(); -builder.Services.AddSingleton(_ => new BackgroundTaskQueue(30)); - var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/src/FakeRelay.Web/Services/QueuedHostedService.cs b/src/FakeRelay.Web/Services/QueuedHostedService.cs deleted file mode 100644 index abf4453..0000000 --- a/src/FakeRelay.Web/Services/QueuedHostedService.cs +++ /dev/null @@ -1,102 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Channels; -using System.Threading.Tasks; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; - -namespace FakeRelay.Web.Services; - -public interface IBackgroundTaskQueue -{ - ValueTask QueueBackgroundWorkItemAsync(Func workItem); - - ValueTask> DequeueAsync( - CancellationToken cancellationToken); -} - -public class BackgroundTaskQueue : IBackgroundTaskQueue -{ - private readonly Channel> _queue; - - public BackgroundTaskQueue(int capacity) - { - // Capacity should be set based on the expected application load and - // number of concurrent threads accessing the queue. - // BoundedChannelFullMode.Wait will cause calls to WriteAsync() to return a task, - // which completes only when space became available. This leads to backpressure, - // in case too many publishers/calls start accumulating. - var options = new BoundedChannelOptions(capacity) - { - FullMode = BoundedChannelFullMode.Wait - }; - _queue = Channel.CreateBounded>(options); - } - - public async ValueTask QueueBackgroundWorkItemAsync( - Func workItem) - { - if (workItem == null) - { - throw new ArgumentNullException(nameof(workItem)); - } - - await _queue.Writer.WriteAsync(workItem); - } - - public async ValueTask> DequeueAsync( - CancellationToken cancellationToken) - { - var workItem = await _queue.Reader.ReadAsync(cancellationToken); - - return workItem; - } -} - -public class QueuedHostedService : BackgroundService -{ - private readonly ILogger _logger; - - public QueuedHostedService(IBackgroundTaskQueue taskQueue, - ILogger logger) - { - TaskQueue = taskQueue; - _logger = logger; - } - - public IBackgroundTaskQueue TaskQueue { get; } - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - _logger.LogInformation( - $"Queued Hosted Service is running."); - - await BackgroundProcessing(stoppingToken); - } - - private async Task BackgroundProcessing(CancellationToken stoppingToken) - { - while (!stoppingToken.IsCancellationRequested) - { - var workItem = - await TaskQueue.DequeueAsync(stoppingToken); - - try - { - await workItem(stoppingToken); - } - catch (Exception ex) - { - _logger.LogError(ex, - "Error occurred executing {WorkItem}.", nameof(workItem)); - } - } - } - - public override async Task StopAsync(CancellationToken stoppingToken) - { - _logger.LogInformation("Queued Hosted Service is stopping."); - - await base.StopAsync(stoppingToken); - } -}