Open History to see the last commit
1import type { ServerEventLoopStall } from "@t3tools/contracts";
2import { mFallback, msg, plural, part } from "gt-i18n";
3import type { M } from "gt-i18n";
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: M = mFallback,
18): HostFreezeNotice | null {
19 if (stall === undefined) return null;
20 const { loadAverage, cpuCount } = stall;
21 return {
22 title: resolve(
23 msg`${{ host: hostLabel }} froze for ${{ seconds: Math.round(stall.durationMs / 1000) }}s`,
24 ),
25 description:
26 loadAverage !== undefined && loadAverage > cpuCount
27 ? resolve(
28 msg`Its CPU is overloaded (load ${{ load: Math.round(loadAverage) }} on ${plural(
29 { cpuCount },
30 {
31 one: part`${{ cpuCount }} core`,
32 other: part`${{ cpuCount }} cores`,
33 },
34 )}).`,
35 )
36 : resolve(msg`T3 Code on ${{ host: hostLabel }} was too busy to answer.`),
37 };
38}