From c88d9b8fd3f95ebbea725536493d411208d3e063 Mon Sep 17 00:00:00 2001 From: Piotrekol <4990365+Piotrekol@users.noreply.github.com> Date: Sat, 16 Dec 2023 21:07:26 +0100 Subject: [PATCH] Misc: Limit all WS connections to 20 messages/s by default --- docs/docs/development/SC/api.md | 2 +- plugins/WebSocketDataSender/WebSocketTokenEndpoint.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/docs/development/SC/api.md b/docs/docs/development/SC/api.md index e841a9b3..37892e86 100644 --- a/docs/docs/development/SC/api.md +++ b/docs/docs/development/SC/api.md @@ -25,7 +25,7 @@ To have access to all current token values you need to cache them: By default token values are sent as soon as specific plugin updates it, resulting in several messages containing information about single map/state change. As this may be sometimes undesirable, SC can be configured to send token updates once all plugins have completed their work by appending `?bulkUpdates=MainPipeline,LiveTokens` to url: @[code js{1}](./apiExamples/minimalWSpt3.js) -Maximum amount of messages sent per second by StreamCompanion can be controlled by appending `?updatesPerSecond=15` to url (adjust number as necessary). +Maximum amount of messages sent per second by StreamCompanion can be controlled by appending `?updatesPerSecond=15` to url (adjust number as necessary). **Limited to 20 by default.** @[code js{1}](./apiExamples/minimalWSpt4.js) ### `outputPatterns` diff --git a/plugins/WebSocketDataSender/WebSocketTokenEndpoint.cs b/plugins/WebSocketDataSender/WebSocketTokenEndpoint.cs index 0fed4a0f..f1124278 100644 --- a/plugins/WebSocketDataSender/WebSocketTokenEndpoint.cs +++ b/plugins/WebSocketDataSender/WebSocketTokenEndpoint.cs @@ -66,7 +66,7 @@ protected override async Task OnClientConnectedAsync(IWebSocketContext context) return; } - contextState.UpdateSleepDelay = (int)Math.Ceiling(1000f / updateRate); + contextState.UpdateSleepDelayMs = (int)Math.Ceiling(1000f / updateRate); } } @@ -107,10 +107,10 @@ public async Task SendLoop(IWebSocketContext context) await SendAsync(context, payload).ConfigureAwait(false); keyValuesToSend.Clear(); - if (state.UpdateSleepDelay > 0) + if (state.UpdateSleepDelayMs > 0) { await updateSleepTask.ConfigureAwait(false); - updateSleepTask = Task.Delay(state.UpdateSleepDelay, context.CancellationToken); + updateSleepTask = Task.Delay(state.UpdateSleepDelayMs, context.CancellationToken); } } } @@ -209,7 +209,7 @@ private class ContextTokensState : IDisposable public List RequestedTokenNames { get; set; } = new(); public ManualResetEventSlim ManualResetEventSlim { get; set; } = new(); public LockingQueue TokensPendingUpdate { get; private set; } = new(); - public int UpdateSleepDelay { get; set; } = -1; + public int UpdateSleepDelayMs { get; set; } = 50; //20 messages/s public void TokenValueUpdated(object _, IToken token) {