Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: add heartbeat to lsp websocket (#13885)

Liu Ji1d2d7446885c
diff --git a/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue b/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
index f5f9b70a2c..cd512a1ee7 100644
--- a/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
@@ -25,9 +25,17 @@
</div>
</template>
<template #default>
- <div class="inline-flex gap-1">
- <span>Language server</span>
- <span>{{ connectionStateText }}</span>
+ <div class="flex flex-col gap-1">
+ <div class="inline-flex gap-1">
+ <span>Language server</span>
+ <span>{{ connectionStateText }}</span>
+ </div>
+ <div
+ v-if="connectionHeartbeatText"
+ class="text-xs text-control-placeholder"
+ >
+ {{ connectionHeartbeatText }}
+ </div>
</div>
</template>
</NPopover>
@@ -35,6 +43,7 @@
</template>
<script lang="ts" setup>
+import dayjs from "dayjs";
import { NPopover } from "naive-ui";
import {
onMounted,
@@ -117,7 +126,7 @@ const containerRef = ref<HTMLDivElement>();
const editorRef = shallowRef<IStandaloneCodeEditor>();
const ready = ref(false);
const contentRef = ref("");
-const { connectionState } = useLSPConnectionState();
+const { connectionState, connectionHeartbeat } = useLSPConnectionState();
const connectionStateIndicatorClass = computed(() => {
const state = connectionState.value;
if (state === "ready") {
@@ -138,6 +147,13 @@ const connectionStateText = computed(() => {
}
return "disconnected";
});
+const connectionHeartbeatText = computed(() => {
+ if (connectionState.value !== "ready") return "";
+ const timestamp = connectionHeartbeat.value?.timestamp;
+ if (!timestamp) return "";
+ const time = dayjs(timestamp).format("YYYY-MM-DD HH:mm:ss.SSS UTCZZ");
+ return `Last heartbeat at ${time}`;
+});
onMounted(async () => {
const container = containerRef.value;
diff --git a/frontend/src/components/MonacoEditor/composables/useLSPConnectionState.ts b/frontend/src/components/MonacoEditor/composables/useLSPConnectionState.ts
index cff51ca47c..4613e8c9f4 100644
--- a/frontend/src/components/MonacoEditor/composables/useLSPConnectionState.ts
+++ b/frontend/src/components/MonacoEditor/composables/useLSPConnectionState.ts
@@ -3,12 +3,18 @@ import type { ConnectionState } from "../lsp-client";
export const useLSPConnectionState = () => {
const connectionState = ref<ConnectionState["state"]>();
+ const connectionHeartbeat = ref<ConnectionState["heartbeat"]>();
- import("../lsp-client").then(({ connectionState: state }) => {
- watchEffect(() => {
- connectionState.value = state.value;
- });
- });
+ import("../lsp-client").then(
+ ({ connectionState: state, connectionHeartbeat: heartbeat }) => {
+ watchEffect(() => {
+ connectionState.value = state.value;
+ });
+ watchEffect(() => {
+ connectionHeartbeat.value = heartbeat.value;
+ });
+ }
+ );
- return { connectionState };
+ return { connectionState, connectionHeartbeat };
};
diff --git a/frontend/src/components/MonacoEditor/lsp-client.ts b/frontend/src/components/MonacoEditor/lsp-client.ts
index 335c9b4c85..1e63bcd57b 100644
--- a/frontend/src/components/MonacoEditor/lsp-client.ts
+++ b/frontend/src/components/MonacoEditor/lsp-client.ts
@@ -1,3 +1,5 @@
+import dayjs from "dayjs";
+import { omit } from "lodash-es";
import {
MonacoLanguageClient,
type IConnectionProvider,
@@ -17,6 +19,7 @@ import {
MAX_RETRIES,
messages,
progressiveDelay,
+ WEBSOCKET_HEARTBEAT_INTERVAL,
WEBSOCKET_TIMEOUT,
} from "./utils";
@@ -26,6 +29,11 @@ export type ConnectionState = {
ws: Promise<WebSocket> | undefined;
lastCommand: ExecuteCommandParams | undefined;
retries: number;
+ heartbeat: {
+ timer: NodeJS.Timeout | undefined;
+ counter: number;
+ timestamp: number;
+ };
};
const conn = shallowReactive<ConnectionState>({
@@ -34,6 +42,11 @@ const conn = shallowReactive<ConnectionState>({
ws: undefined,
lastCommand: undefined,
retries: 0,
+ heartbeat: shallowReactive({
+ timer: undefined,
+ counter: 0,
+ timestamp: 0,
+ }),
});
const connectWebSocket = () => {
@@ -91,6 +104,7 @@ const connectWebSocket = () => {
}
conn.state = "ready";
conn.retries = 0; // reset retry counter
+ useHeartbeat(ws);
resolve(ws);
});
ws.addEventListener("close", (e) => {
@@ -239,4 +253,40 @@ export const executeCommand = async (
return result;
};
+const useHeartbeat = (ws: WebSocket) => {
+ const cleanup = () => {
+ clearTimeout(conn.heartbeat.timer);
+ conn.heartbeat = {
+ timer: undefined,
+ counter: 0,
+ timestamp: 0,
+ };
+ };
+
+ ws.addEventListener("error", cleanup);
+ ws.addEventListener("close", cleanup);
+
+ const ping = () => {
+ conn.heartbeat.counter++;
+ conn.heartbeat.timestamp = Date.now();
+ ws.send(
+ JSON.stringify({
+ jsonrpc: "2.0",
+ method: "$ping",
+ params: {
+ state: omit(conn.heartbeat, "timer"),
+ },
+ })
+ );
+ console.debug(
+ `[LSP-Client] counter:${conn.heartbeat.counter} timestamp:${conn.heartbeat.timestamp}(${dayjs(conn.heartbeat.timestamp).toString()})`
+ );
+
+ conn.heartbeat.timer = setTimeout(ping, WEBSOCKET_HEARTBEAT_INTERVAL);
+ };
+
+ ping();
+};
+
export const connectionState = toRef(conn, "state");
+export const connectionHeartbeat = toRef(conn, "heartbeat");
diff --git a/frontend/src/components/MonacoEditor/utils.ts b/frontend/src/components/MonacoEditor/utils.ts
index 39d8d1d22d..4029daeb28 100644
--- a/frontend/src/components/MonacoEditor/utils.ts
+++ b/frontend/src/components/MonacoEditor/utils.ts
@@ -19,6 +19,7 @@ export const RECONNECTION_DELAY = {
};
// Timeout to setup connection in EACH attempt
export const WEBSOCKET_TIMEOUT = 5000;
+export const WEBSOCKET_HEARTBEAT_INTERVAL = 10 * 1000; // 10 seconds
export const messages = {
title: () => t("sql-editor.web-socket.errors.title"),