Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Implemented Throttle Delay of 1 sec for Workflow Socket Messages #899

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions frontend/src/components/helpers/socket-messages/SocketMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function SocketMessages() {
// Buffer to hold the logs between throttle intervals
const psLogs = useRef([]);
const wfLogs = useRef([]);
// Buffer to hold the staged messages between throttle intervals
const stagedMsgsBuffer = useRef([]);

useEffect(() => {
setLogId(sessionDetails?.logEventsId || "");
Expand All @@ -53,13 +55,24 @@ function SocketMessages() {
}, THROTTLE_DELAY)
).current;

// Throttled function for staged messages
const stagedMsgsThrottledUpdate = useRef(
throttle((bufferedMsgs) => {
bufferedMsgs.forEach((msg) => {
pushStagedMessage(msg);
});
stagedMsgsBuffer.current = [];
}, 2000)
).current;

// Clean up throttling functions on unmount
useEffect(() => {
return () => {
psLogsThrottledUpdate.cancel();
wfLogsThrottledUpdate.cancel();
stagedMsgsThrottledUpdate.cancel();
};
}, [psLogsThrottledUpdate, wfLogsThrottledUpdate]);
}, [psLogsThrottledUpdate, wfLogsThrottledUpdate, stagedMsgsThrottledUpdate]);

const handlePsLogs = useCallback(
(msg) => {
Expand All @@ -76,10 +89,20 @@ function SocketMessages() {
},
[wfLogsThrottledUpdate]
);

const handleStagedMessages = useCallback(
(msg) => {
stagedMsgsBuffer.current = [...stagedMsgsBuffer.current, msg];
stagedMsgsThrottledUpdate(stagedMsgsBuffer.current);
},
[stagedMsgsThrottledUpdate]
);

// Handle incoming socket messages
const onMessage = (data) => {
try {
let msg = data.data;

// Attempt to decode data as JSON if it's in encoded state
if (typeof msg === "string" || msg instanceof Uint8Array) {
if (typeof msg === "string") {
Expand All @@ -95,7 +118,7 @@ function SocketMessages() {
msg.message = msg?.log;
handleWfLogs(msg);
} else if (msg?.type === "UPDATE") {
pushStagedMessage(msg);
handleStagedMessages(msg);
} else if (msg?.type === "LOG" && msg?.service === "prompt") {
handlePsLogs(msg);
}
Expand Down
Loading