use opened promise for tcp connection test

pull/8/head
lyc8503 2024-01-30 13:28:30 +08:00
parent b6702e9958
commit 31a4d9345a
1 changed files with 4 additions and 10 deletions

View File

@ -17,18 +17,12 @@ export async function getStatus(
// TCP port endpoint monitor
try {
const [hostname, port] = monitor.target.split(':')
// Write "PING\n"
const socket = connect({ hostname: hostname, port: Number(port) })
const writer = socket.writable.getWriter()
const reader = socket.readable.getReader()
await withTimeout(monitor.timeout || 10000, writer.write(new TextEncoder().encode('PING\n')))
await withTimeout(monitor.timeout || 10000, reader.read())
await socket.close()
// Not having an `opened` promise now...
// https://github.com/cloudflare/workerd/issues/1305
// await withTimeout(monitor.timeout || 10000, socket.closed)
// Now we have an `opened` promise!
// @ts-ignore
await withTimeout(monitor.timeout || 10000, socket.opened)
await socket.close()
console.log(`${monitor.name} connected to ${monitor.target}`)