diff --git a/Iceshrimp.Backend/Core/Services/StreamingService.cs b/Iceshrimp.Backend/Core/Services/StreamingService.cs index 17d4d4f3..52128b39 100644 --- a/Iceshrimp.Backend/Core/Services/StreamingService.cs +++ b/Iceshrimp.Backend/Core/Services/StreamingService.cs @@ -1,5 +1,4 @@ using System.Collections.Concurrent; -using AsyncKeyedLock; using Iceshrimp.Backend.Controllers.Web.Renderers; using Iceshrimp.Backend.Core.Database.Tables; using Iceshrimp.Backend.SignalR; @@ -12,12 +11,6 @@ namespace Iceshrimp.Backend.Core.Services; public sealed class StreamingService { - private static readonly AsyncKeyedLocker Locker = new(o => - { - o.PoolSize = 100; - o.PoolInitialFill = 5; - }); - private readonly ConcurrentDictionary _connections = []; private readonly EventService _eventSvc; private readonly IHubContext _hub; @@ -40,8 +33,8 @@ public sealed class StreamingService eventSvc.NoteUpdated += OnNoteUpdated; } - public event EventHandler<(Note note, Func> rendered)>? NotePublished; - public event EventHandler<(Note note, Func> rendered)>? NoteUpdated; + public event EventHandler<(Note note, Lazy> rendered)>? NotePublished; + public event EventHandler<(Note note, Lazy> rendered)>? NoteUpdated; public void Connect(string userId, User user, string connectionId) { @@ -80,23 +73,14 @@ public sealed class StreamingService return Task.CompletedTask; } - private Func> Render(Note note) + private Lazy> Render(Note note) { - NoteResponse? res = null; - return async () => + return new Lazy>(async () => { - // Skip the mutex if res is already set - if (res != null) return res; - - using (await Locker.LockAsync(note.Id)) - { - if (res != null) return res; - await using var scope = _scopeFactory.CreateAsyncScope(); - var renderer = scope.ServiceProvider.GetRequiredService(); - res = await renderer.RenderOne(note, null); - return res; - } - }; + await using var scope = _scopeFactory.CreateAsyncScope(); + var renderer = scope.ServiceProvider.GetRequiredService(); + return await renderer.RenderOne(note, null); + }); } private void OnNotePublished(object? _, Note note) diff --git a/Iceshrimp.Backend/SignalR/Helpers/StreamingConnectionAggregate.cs b/Iceshrimp.Backend/SignalR/Helpers/StreamingConnectionAggregate.cs index 669cd64e..9d6ecc84 100644 --- a/Iceshrimp.Backend/SignalR/Helpers/StreamingConnectionAggregate.cs +++ b/Iceshrimp.Backend/SignalR/Helpers/StreamingConnectionAggregate.cs @@ -84,7 +84,7 @@ public sealed class StreamingConnectionAggregate : IDisposable } } - private async void OnNotePublished(object? _, (Note note, Func> rendered) data) + private async void OnNotePublished(object? _, (Note note, Lazy> rendered) data) { try { @@ -100,7 +100,7 @@ public sealed class StreamingConnectionAggregate : IDisposable return; } - var rendered = EnforceRenoteReplyVisibility(await data.rendered(), wrapped); + var rendered = EnforceRenoteReplyVisibility(await data.rendered.Value, wrapped); await _hub.Clients.Clients(recipients.connectionIds).NotePublished(recipients.timelines, rendered); } catch (Exception e) @@ -109,7 +109,7 @@ public sealed class StreamingConnectionAggregate : IDisposable } } - private async void OnNoteUpdated(object? _, (Note note, Func> rendered) data) + private async void OnNoteUpdated(object? _, (Note note, Lazy> rendered) data) { try { @@ -118,7 +118,7 @@ public sealed class StreamingConnectionAggregate : IDisposable var (connectionIds, _) = FindRecipients(data.note); if (connectionIds.Count == 0) return; - var rendered = EnforceRenoteReplyVisibility(await data.rendered(), wrapped); + var rendered = EnforceRenoteReplyVisibility(await data.rendered.Value, wrapped); await _hub.Clients.Clients(connectionIds).NoteUpdated(rendered); } catch (Exception e)