Skip to content

Commit

Permalink
min possible streaming interval is 10ms mockoon#83
Browse files Browse the repository at this point in the history
  • Loading branch information
isuru89 committed Jan 27, 2024
1 parent 56a0651 commit 8c4675d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/commons-server/src/libs/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import { CrudRouteIds, crudRoutesBuilder, databucketActions } from './crud';
import {
BroadcastContext,
DelegatedBroadcastHandler,
getSafeStreamingInterval,
isWebSocketOpen,
messageToString,
serveFileContentInWs,
Expand Down Expand Up @@ -844,7 +845,6 @@ export class MockoonServer extends (EventEmitter as new () => TypedEmitter<Serve
* @param route
* @param request
* @param baseErrorMeta
* @returns setInterval reference, so that it can be cleared when socket closes.
*/
private handleOneToOneStreamingResponses(
socket: WebSocket,
Expand Down Expand Up @@ -893,7 +893,7 @@ export class MockoonServer extends (EventEmitter as new () => TypedEmitter<Serve
this.serveWsResponse(socket, content, errorMetaData);
}
}
}, route.streamingInterval);
}, getSafeStreamingInterval(route.streamingInterval));

socket.on('close', () => {
// close any interval data pushes
Expand Down
11 changes: 10 additions & 1 deletion packages/commons-server/src/libs/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import TypedEventEmitter from 'typed-emitter';
import { RawData, WebSocket } from 'ws';
import { WebSocketResponseRulesInterpreter } from '../ws-response-rules-interpreter';

const SMALLEST_POSSIBLE_STREAMING_INTERVAL = 10;

export type DelegatedTemplateParser = (content: string) => string;

export type DelegatedBroadcastHandler = (
Expand All @@ -26,6 +28,13 @@ export class ServerContext {
) {}
}

/**
* Returns a safe streaming interval for one-to-one and broadcast websockets.
* Having a small interval could make server potentially unusable.
*/
export const getSafeStreamingInterval = (givenInterval: number): number =>
Math.max(SMALLEST_POSSIBLE_STREAMING_INTERVAL, givenInterval);

/**
* Represents a single running streaming data for a route.
* For all socket clients, there will be only one single instance.
Expand Down Expand Up @@ -57,7 +66,7 @@ class WsRunningInstance {
responseNumber += 1;

this.handler(responseNumber, enabledRouteResponse);
}, this.route.streamingInterval);
}, getSafeStreamingInterval(this.route.streamingInterval));
}

public close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@
}"
formControlName="streamingInterval"
/>
<div
*ngIf="
10 > (activeRouteForm.get(['streamingInterval']).value || 0)
"
class="warning-message ms-auto text-center ps-2 align-self-center"
id="disabled-rules-warning-message"
[ngbTooltip]="texts.MINIMUM_STREAMING_INTERVAL_WARNING"
>
<app-svg icon="warning" class="pe-1"></app-svg>
</div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const Texts = {
DISABLED_RULES_WARNING:
'Rules disabled by one of the response modes (sequential, random, rules disabled) or when in streaming mode'
'Rules disabled by one of the response modes (sequential, random, rules disabled) or when in streaming mode',
MINIMUM_STREAMING_INTERVAL_WARNING: 'Minimum interval supported is 10ms.'
};

0 comments on commit 8c4675d

Please sign in to comment.