Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

hostFreeze.ts

38 lines1.2 KB

1import type { ServerEventLoopStall } from "@t3tools/contracts";
2import { mFallback, msg } from "gt-i18n";
3import type { MFunctionType } from "gt-i18n/types";
4​
5export interface HostFreezeNotice {
6 readonly title: string;
7 readonly description: string;
8}
9​
10/**
11 * Says why a host stopped answering, from the recent stall it reports on
12 * connect. Web shows it as a toast, mobile in the environment's connection row.
13 */
14export function hostFreezeNotice(
15 hostLabel: string,
16 stall: ServerEventLoopStall | undefined,
17 resolve: MFunctionType = mFallback,
18): HostFreezeNotice | null {
19 if (stall === undefined) return null;
20 const { loadAverage, cpuCount } = stall;
21 return {
22 title: resolve(
23 msg("{host} froze for {seconds}s", {
24 host: hostLabel,
25 seconds: Math.round(stall.durationMs / 1000),
26 }),
27 ),
28 description:
29 loadAverage !== undefined && loadAverage > cpuCount
30 ? resolve(
31 msg("Its CPU is overloaded (load {load} on {cpuCount} cores).", {
32 load: Math.round(loadAverage),
33 cpuCount,
34 }),
35 )
36 : resolve(msg("T3 Code on {host} was too busy to answer.", { host: hostLabel })),
37 };