Skip to content

Commit

Permalink
🔀 adds abort functionality to SSE in PodLogs.tsx (cyclops-ui#597)
Browse files Browse the repository at this point in the history
* fix: adds abort functionality to PodLogs.tsx to prevent open connections and duplicated logs

* remove empty comment

---------

Co-authored-by: petar-cvit <[email protected]>
  • Loading branch information
naineel1209 and petar-cvit authored Oct 5, 2024
1 parent 251bcb2 commit 29fdbef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReadOutlined } from "@ant-design/icons";
import { Alert, Button, Col, Divider, Modal, Tabs, TabsProps } from "antd";
import { useState } from "react";
import { useRef, useState } from "react";
import ReactAce from "react-ace/lib/ace";
import { mapResponseError } from "../../../../utils/api/errors";
import { isStreamingEnabled } from "../../../../utils/api/common";
Expand All @@ -20,6 +20,7 @@ const PodLogs = ({ pod }: PodLogsProps) => {
containers: [],
initContainers: [],
});
const logsSignalControllerRef = useRef<AbortController | null>(null);

const [error, setError] = useState({
message: "",
Expand All @@ -35,6 +36,11 @@ const PodLogs = ({ pod }: PodLogsProps) => {
initContainers: [],
});
setLogs([]);

// send the abort signal
if (logsSignalControllerRef.current !== null) {
logsSignalControllerRef.current.abort();
}
};

const getTabItems = () => {
Expand Down Expand Up @@ -107,6 +113,10 @@ const PodLogs = ({ pod }: PodLogsProps) => {

const onLogsTabsChange = (container: string) => {
const controller = new AbortController();
if (logsSignalControllerRef.current !== null) {
logsSignalControllerRef.current.abort();
}
logsSignalControllerRef.current = controller; // store the controller to be able to abort the request
setLogs(() => []);

if (isStreamingEnabled()) {
Expand Down Expand Up @@ -179,6 +189,8 @@ const PodLogs = ({ pod }: PodLogsProps) => {
onClick={function () {
if (isStreamingEnabled()) {
const controller = new AbortController();
logsSignalControllerRef.current = controller; // store the controller to be able to abort the request

logStream(
pod.name,
pod.namespace,
Expand Down
2 changes: 1 addition & 1 deletion cyclops-ui/src/utils/api/sse/logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function logStream(
response.ok &&
response.headers.get("content-type") === EventStreamContentType
) {
// here we have success - we need to remove the error and reset the logs
// here we have success - we need to remove the error and reset the logs
setError(new Error(), true);
setLog("", true);
return;
Expand Down

0 comments on commit 29fdbef

Please sign in to comment.