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

Perf: Add SFTP Heart Beat #1621

Merged
merged 1 commit into from
Jan 6, 2025
Merged
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
55 changes: 51 additions & 4 deletions ui/src/hooks/useFileManage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,39 @@ const handleSocketSftpData = (messageData: IFileManageSftpFileItem[]) => {
fileManageStore.setFileList(messageData);
};

/**
* @description 心跳检测机制
* @param socket WebSocket实例
*/
const heartBeat = (socket: WebSocket) => {
let pingInterval: number | null = null;

const sendPing = () => {
if (socket.CLOSED === socket.readyState || socket.CLOSING === socket.readyState) {
clearInterval(pingInterval!);
return;
}

const pingMessage = {
id: uuid(),
type: MessageType.PING,
data: 'ping'
};

socket.send(JSON.stringify(pingMessage));
};

sendPing();

pingInterval = window.setInterval(sendPing, 2000);

return () => {
if (pingInterval) {
clearInterval(pingInterval);
}
};
};

/**
* @description 处理 message
* @param socket
Expand All @@ -144,13 +177,14 @@ const initSocketEvent = (socket: WebSocket) => {
const fileManageStore = useFileManageStore();

let receivedBuffers: any = [];
let clearHeartbeat: (() => void) | null = null;

socket.binaryType = 'arraybuffer';

socket.onopen = () => {};
socket.onerror = () => {};

socket.onclose = () => {};
socket.onopen = () => { clearHeartbeat = heartBeat(socket) };
socket.onerror = () => { clearHeartbeat?.() };
socket.onclose = () => { clearHeartbeat?.() };

socket.onmessage = (event: MessageEvent) => {
const message: IFileManage = JSON.parse(event.data);

Expand Down Expand Up @@ -238,6 +272,19 @@ const initSocketEvent = (socket: WebSocket) => {
break;
}

case MessageType.PING: {
socket.send(JSON.stringify({
id: uuid(),
type: MessageType.PONG,
data: 'pong'
}));
break;
}

case MessageType.PONG: {
break;
}

default: {
break;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unable to assess the code because it is cut off with "const fileManageStore = useFileManageStore();". Please ensure that the whole content provided here includes fileManageStore, otherwise further analyses will not be valid.

If there's an issue mentioned, please provide more details so I could help you better.

Expand Down
Loading