Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix: add WebSocket keepalive pings and DB connection timeouts (#1323)

shockalotti6ae96c6d6fbb
diff --git a/.husky/commit-msg b/.husky/commit-msg
index c48ae6930..638e85095 100644
--- a/.husky/commit-msg
+++ b/.husky/commit-msg
@@ -1 +1 @@
-pnpm dlx commitlint --edit $1
+pnpm exec commitlint --edit $1
diff --git a/apps/api/src/database/index.ts b/apps/api/src/database/index.ts
index ea2cbda62..1c5764b0e 100644
--- a/apps/api/src/database/index.ts
+++ b/apps/api/src/database/index.ts
@@ -135,6 +135,11 @@ export function getDatabasePool(): Pool {
if (!pool) {
pool = new Pool({
connectionString: resolveDatabaseConnectionString(),
+ // Fail fast when Railway's internal network is slow rather than hanging
+ // indefinitely and blocking every API request.
+ connectionTimeoutMillis: 5_000,
+ idleTimeoutMillis: 30_000,
+ max: 10,
});
}
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 07dc093b3..b2e874c45 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -586,6 +586,27 @@ export function createApp() {
conn = addConnection(projectId, ws, userId, initiatorId);
}
},
+ onMessage(evt) {
+ // Respond to client keepalive pings (sent every 30s to prevent
+ // Cloudflare from closing idle connections at 100s timeout)
+ try {
+ const raw =
+ typeof evt.data === "string"
+ ? evt.data
+ : Buffer.isBuffer(evt.data)
+ ? evt.data.toString()
+ : null;
+ if (raw) {
+ const msg = JSON.parse(raw) as { type?: string };
+ if (msg?.type === "ping") {
+ // No-op: receiving the ping is enough to satisfy Cloudflare.
+ // A pong response is optional but helps confirm liveness.
+ }
+ }
+ } catch {
+ // Ignore malformed messages
+ }
+ },
onClose() {
if (conn && projectId) {
removeConnection(projectId, conn);
diff --git a/apps/web/src/hooks/queries/task/use-get-task.ts b/apps/web/src/hooks/queries/task/use-get-task.ts
index 0dadc5ad0..336a3ccf8 100644
--- a/apps/web/src/hooks/queries/task/use-get-task.ts
+++ b/apps/web/src/hooks/queries/task/use-get-task.ts
@@ -8,6 +8,7 @@ function useGetTask(taskId: string) {
enabled: Boolean(taskId),
refetchOnMount: "always",
staleTime: 0,
+ enabled: Boolean(taskId),
});
}
diff --git a/apps/web/src/hooks/use-project-websocket.ts b/apps/web/src/hooks/use-project-websocket.ts
index 4f91fe675..8b41317ac 100644
--- a/apps/web/src/hooks/use-project-websocket.ts
+++ b/apps/web/src/hooks/use-project-websocket.ts
@@ -13,18 +13,30 @@ export function getWsUrl(projectId: string) {
const MAX_RETRIES = 5;
const BASE_DELAY = 1000; // 1 second
+// Cloudflare closes idle WebSocket connections after 100 seconds of no traffic.
+// We send a lightweight ping every 30 seconds to keep the connection alive.
+const WS_PING_INTERVAL_MS = 30_000;
+
export function useProjectWebSocket(projectId: string) {
const queryClient = useQueryClient();
const { data: session } = authClient.useSession();
const wsRef = useRef<WebSocket | null>(null);
const retriesRef = useRef(0);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const pingIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
useEffect(() => {
if (!projectId || !session?.user?.id) return;
retriesRef.current = 0;
+ function clearPing() {
+ if (pingIntervalRef.current) {
+ clearInterval(pingIntervalRef.current);
+ pingIntervalRef.current = null;
+ }
+ }
+
function connect() {
const url = getWsUrl(projectId);
const ws = new WebSocket(url);
@@ -32,6 +44,13 @@ export function useProjectWebSocket(projectId: string) {
ws.onopen = () => {
retriesRef.current = 0; // Reset retries on successful connection
+ // Start keepalive pings to prevent Cloudflare idle timeout (100s)
+ clearPing();
+ pingIntervalRef.current = setInterval(() => {
+ if (ws.readyState === WebSocket.OPEN) {
+ ws.send(JSON.stringify({ type: "ping" }));
+ }
+ }, WS_PING_INTERVAL_MS);
};
ws.onmessage = (event) => {
@@ -99,6 +118,7 @@ export function useProjectWebSocket(projectId: string) {
};
ws.onclose = () => {
+ clearPing();
wsRef.current = null;
if (retriesRef.current < MAX_RETRIES) {
@@ -112,6 +132,7 @@ export function useProjectWebSocket(projectId: string) {
return () => {
retriesRef.current = MAX_RETRIES; // Prevent reconnect after unmount
+ clearPing();
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}