Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54ddd8b

Browse files
committed
linter changes and refactored alive() to Heart
1 parent 42485fb commit 54ddd8b

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

‎src/node/app/health.ts‎

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,27 @@ import { HttpProvider, HttpResponse, Route, Heart, HttpProviderOptions } from ".
66
* Check the heartbeat.
77
*/
88
export class HealthHttpProvider extends HttpProvider {
9+
public constructor(options: HttpProviderOptions, private readonly heart: Heart) {
10+
super(options)
11+
}
912

10-
public constructor(
11-
options: HttpProviderOptions,
12-
private readonly heart: Heart
13-
) {
14-
super(options)
13+
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
14+
if (!this.authenticated(request)) {
15+
if (this.isRoot(route)) {
16+
return { redirect: "/login", query: { to: route.fullPath } }
17+
}
18+
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
1519
}
1620

17-
private alive(): Boolean {
18-
const now = Date.now()
19-
return (now - this.heart.lastHeartbeat < this.heart.heartbeatInterval)
21+
const result = {
22+
cache: false,
23+
mime: "application/json",
24+
content: {
25+
status: this.heart.alive() ? "alive" : "expired",
26+
lastHeartbeat: this.heart.lastHeartbeat,
27+
},
2028
}
2129

22-
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
23-
if (!this.authenticated(request)) {
24-
if (this.isRoot(route)) {
25-
return { redirect: "/login", query: { to: route.fullPath } }
26-
}
27-
throw new HttpError("Unauthorized", HttpCode.Unauthorized)
28-
}
29-
30-
const result = {
31-
cache: false,
32-
mime: 'application/json',
33-
content: {
34-
status: (this.alive()) ? 'alive' : 'expired',
35-
lastHeartbeat: this.heart.lastHeartbeat
36-
37-
}
38-
}
39-
40-
return result
41-
42-
}
30+
return result
31+
}
4332
}

‎src/node/entry.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { field, logger } from "@coder/logger"
22
import * as cp from "child_process"
33
import * as path from "path"
44
import { CliMessage } from "../../lib/vscode/src/vs/server/ipc"
5+
import { plural } from "../common/util"
6+
import { HealthHttpProvider } from "./app/health"
57
import { LoginHttpProvider } from "./app/login"
68
import { ProxyHttpProvider } from "./app/proxy"
79
import { StaticHttpProvider } from "./app/static"
810
import { UpdateHttpProvider } from "./app/update"
911
import { VscodeHttpProvider } from "./app/vscode"
10-
import { HealthHttpProvider } from "./app/health"
1112
import { Args, bindAddrFromAllSources, optionDescriptions, parse, readConfigFile, setDefaults } from "./cli"
1213
import { AuthType, HttpServer, HttpServerOptions } from "./http"
1314
import { loadPlugins } from "./plugin"
1415
import { generateCertificate, hash, humanPath, open } from "./util"
1516
import { ipcMain, wrap } from "./wrapper"
16-
import { plural } from "../common/util"
1717

1818
process.on("uncaughtException", (error) => {
1919
logger.error(`Uncaught exception: ${error.message}`)

‎src/node/http.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,24 +395,27 @@ export abstract class HttpProvider {
395395
*/
396396
export class Heart {
397397
private heartbeatTimer?: NodeJS.Timeout
398-
public heartbeatInterval = 60000
398+
private heartbeatInterval = 60000
399399
public lastHeartbeat = 0
400400

401401
public constructor(private readonly heartbeatPath: string, private readonly isActive: () => Promise<boolean>) {}
402402

403+
public alive(): boolean {
404+
const now = Date.now()
405+
return now - this.lastHeartbeat < this.heartbeatInterval
406+
}
403407
/**
404408
* Write to the heartbeat file if we haven't already done so within the
405409
* timeout and start or reset a timer that keeps running as long as there is
406410
* activity. Failures are logged as warnings.
407411
*/
408412
public beat(): void {
409-
const now = Date.now()
410-
if (now - this.lastHeartbeat >= this.heartbeatInterval) {
413+
if (!this.alive()) {
411414
logger.trace("heartbeat")
412415
fs.outputFile(this.heartbeatPath, "").catch((error) => {
413416
logger.warn(error.message)
414417
})
415-
this.lastHeartbeat = now
418+
this.lastHeartbeat = Date.now()
416419
if (typeof this.heartbeatTimer !== "undefined") {
417420
clearTimeout(this.heartbeatTimer)
418421
}
@@ -603,8 +606,9 @@ export class HttpServer {
603606

604607
private onRequest = async (request: http.IncomingMessage, response: http.ServerResponse): Promise<void> => {
605608
const route = this.parseUrl(request)
606-
if (route.providerBase !== '/healthz')
607-
this.heart.beat()
609+
if (route.providerBase !== "/healthz") {
610+
this.heart.beat()
611+
}
608612
const write = (payload: HttpResponse): void => {
609613
response.writeHead(payload.redirect ? HttpCode.Redirect : payload.code || HttpCode.Ok, {
610614
"Content-Type": payload.mime || getMediaMime(payload.filePath),

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /