-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Fix healthz socket #2737
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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 | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
|
@@ -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. | ||
if (this.sockets.size > 0) { | ||
// Give sockets a chance to close up shop. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
}) | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great comments! 👏