Skip to content

Commit

Permalink
Misc: Limit all WS connections to 20 messages/s by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed Dec 16, 2023
1 parent 5272666 commit c88d9b8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/docs/development/SC/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
8 changes: 4 additions & 4 deletions plugins/WebSocketDataSender/WebSocketTokenEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ private class ContextTokensState : IDisposable
public List<string> RequestedTokenNames { get; set; } = new();
public ManualResetEventSlim ManualResetEventSlim { get; set; } = new();
public LockingQueue<IToken> 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)
{
Expand Down

0 comments on commit c88d9b8

Please sign in to comment.