Skip to content

Fix healthz socket #2737

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

Merged
merged 3 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/node/routes/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ router.get("/", (req, res) => {
export const wsRouter = WsRouter()

wsRouter.ws("/", async (req) => {
wss.handleUpgrade(req, req.socket, req.head, (ws) => {
ws.on("message", () => {
wss.handleUpgrade(req, req.ws, req.head, (ws) => {
ws.addEventListener("message", () => {
ws.send(
JSON.stringify({
event: "health",
Expand All @@ -23,5 +23,6 @@ wsRouter.ws("/", async (req) => {
}),
)
})
req.ws.resume()
})
})
40 changes: 40 additions & 0 deletions test/health.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as httpserver from "./httpserver"
import * as integration from "./integration"

describe("health", () => {
let codeServer: httpserver.HttpServer | undefined

afterEach(async () => {
if (codeServer) {
await codeServer.close()
codeServer = undefined
}
})

it("/healthz", async () => {
;[, , codeServer] = await integration.setup(["--auth=none"], "")
const resp = await codeServer.fetch("/healthz")
expect(resp.status).toBe(200)
const json = await resp.json()
expect(json).toStrictEqual({ lastHeartbeat: 0, status: "expired" })
})

it("/healthz (websocket)", async () => {
;[, , codeServer] = await integration.setup(["--auth=none"], "")
const ws = codeServer.ws("/healthz")
const message = await new Promise((resolve, reject) => {
ws.on("error", console.error)
ws.on("message", (message) => {
try {
const j = JSON.parse(message.toString())
resolve(j)
} catch (error) {
reject(error)
}
})
ws.on("open", () => ws.send(JSON.stringify({ event: "health" })))
})
ws.terminate()
expect(message).toStrictEqual({ event: "health", status: "expired", lastHeartbeat: 0 })
})
})
36 changes: 30 additions & 6 deletions test/httpserver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as express from "express"
import * as http from "http"
import * as net from "net"
import * as nodeFetch from "node-fetch"
import Websocket from "ws"
import * as util from "../src/common/util"
Expand All @@ -8,13 +9,21 @@ import { handleUpgrade } from "../src/node/wsRouter"

// Perhaps an abstraction similar to this should be used in app.ts as well.
export class HttpServer {
private hs = http.createServer()
private readonly sockets = new Set<net.Socket>()
private cleanupTimeout?: NodeJS.Timeout

public constructor(hs?: http.Server) {
// See usage in test/integration.ts
if (hs) {
this.hs = hs
}
// See usage in test/integration.ts
public constructor(private readonly hs = http.createServer()) {
this.hs.on("connection", (socket) => {
this.sockets.add(socket)
socket.on("close", () => {
this.sockets.delete(socket)
if (this.cleanupTimeout && this.sockets.size === 0) {
clearTimeout(this.cleanupTimeout)
this.cleanupTimeout = undefined
}
})
})
}

/**
Expand Down Expand Up @@ -54,13 +63,28 @@ export class HttpServer {
*/
public close(): Promise<void> {
return new Promise((res, rej) => {
// Close will not actually close anything; it just waits until everything
// is closed.
this.hs.close((err) => {
if (err) {
rej(err)
return
}
res()
})

// If there are sockets remaining we might need to force close them or
// this promise might never resolve.
Comment on lines +76 to +77
Copy link
Contributor

Choose a reason for hiding this comment

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

Great comments! 👏

if (this.sockets.size > 0) {
// Give sockets a chance to close up shop.
Copy link
Contributor

Choose a reason for hiding this comment

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

😎

this.cleanupTimeout = setTimeout(() => {
this.cleanupTimeout = undefined
for (const socket of this.sockets.values()) {
console.warn("a socket was left hanging")
socket.destroy()
}
}, 1000)
}
})
}

Expand Down
3 changes: 2 additions & 1 deletion test/test-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const plugin: cs.Plugin = {
wsRouter() {
const wr = cs.WsRouter()
wr.ws("/test-app", (req) => {
cs.wss.handleUpgrade(req, req.socket, req.head, (ws) => {
cs.wss.handleUpgrade(req, req.ws, req.head, (ws) => {
req.ws.resume()
ws.send("hello")
})
})
Expand Down