Skip to content

Commit

Permalink
add tcp timeout, fix worker stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
lyc8503 committed Oct 28, 2023
1 parent 4817fef commit 6157bc0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
1 change: 0 additions & 1 deletion components/DetailBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MonitorState, MonitorTarget } from "@/uptime.types";
import { Box, Tooltip } from "@mantine/core";
import { useEffect, useRef } from "react";


export default function DetailBar({ monitor, state }: { monitor: MonitorTarget, state: MonitorState }) {
Expand Down
4 changes: 2 additions & 2 deletions components/MonitorDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default function MonitorDetail({ monitor, state }: { monitor: MonitorTarg

if (!state.latency[monitor.id]) return (
<>
<Text>{monitor.name}</Text>
<Text>No data available, please make sure you have deployed your workers with latest config and check your worker status!</Text>
<Text mt='sm' fw={700}>{monitor.name}</Text>
<Text mt='sm' fw={700}>No data available, please make sure you have deployed your workers with latest config and check your worker status!</Text>
</>
)

Expand Down
4 changes: 2 additions & 2 deletions worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { connect } from 'cloudflare:sockets'
import config from '../../uptime.config'
import { fetchTimeout, getWorkerLocation } from './util'
import { fetchTimeout, getWorkerLocation, withTimeout } from './util'
import { MonitorState, MonitorTarget } from "../../uptime.types"

export interface Env {
Expand Down Expand Up @@ -30,7 +30,7 @@ async function getStatus(monitor: MonitorTarget): Promise<{ ping: number; up: bo
// Can't do this: await socket.close()

// https://github.com/cloudflare/workerd/issues/1305
await socket.closed
await withTimeout(monitor.timeout || 10000, socket.closed)

console.log(`${monitor.name} connected to ${monitor.target}`)

Expand Down
12 changes: 11 additions & 1 deletion worker/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ const fetchTimeout = (url: string, ms: number, { signal, ...options }: RequestIn
return promise.finally(() => clearTimeout(timeout))
}

export { getWorkerLocation, fetchTimeout }
function withTimeout<T> (millis: number, promise: Promise<T>): Promise<T> {
const timeout = new Promise<T>((resolve, reject) =>
setTimeout(() => reject(new Error(`Promise timed out after ${millis}ms`)), millis))

return Promise.race([
promise,
timeout
])
}

export { getWorkerLocation, fetchTimeout, withTimeout }

0 comments on commit 6157bc0

Please sign in to comment.