Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/conat/core/sys.ts
1542 views
1
import { type Client } from "./client";
2
import type { ConnectionStats } from "./types";
3
4
// requests go to *all* nodes in the cluster
5
// sys.conat.[clusterName]
6
// or sys.conat.default if there is no cluster.
7
export function sysApiSubject({
8
clusterName,
9
id,
10
}: {
11
clusterName?: string;
12
id?: string;
13
}) {
14
if (!id) {
15
return `sys.conat.${clusterName ?? "default"}`;
16
} else {
17
return `sys.conat.${clusterName ?? "default"}.${id}`;
18
}
19
}
20
21
export interface SysConatServer {
22
stats: () => Promise<{ [id: string]: { [id: string]: ConnectionStats } }>;
23
usage: () => Promise<{
24
[id: string]: { total: number; perUser: { [user: string]: number } };
25
}>;
26
disconnect: (ids: string | string[]) => Promise<void>;
27
join: (address: string) => Promise<void>;
28
unjoin: (opts: { clusterName?: string; id: string }) => Promise<void>;
29
clusterTopology: () => Promise<{
30
[clusterName: string]: { [id: string]: string };
31
}>;
32
clusterAddresses: (clusterName?: string) => Promise<string[]>;
33
}
34
35
export interface SysConatServerCallMany {
36
stats: () => Promise<{ [id: string]: { [id: string]: ConnectionStats } }[]>;
37
usage: () => Promise<
38
{
39
[id: string]: { total: number; perUser: { [user: string]: number } };
40
}[]
41
>;
42
disconnect: (ids: string | string[]) => Promise<void>;
43
}
44
45
export function sysApi(client: Client, opts?): SysConatServer {
46
if (client.info == null) {
47
throw Error("client must be signed in");
48
}
49
const subject = sysApiSubject(client.info);
50
return client.call<SysConatServer>(subject, opts);
51
}
52
53
export function sysApiMany(client: Client, opts?): SysConatServerCallMany {
54
if (client.info == null) {
55
throw Error("client must be signed in");
56
}
57
const subject = sysApiSubject({ clusterName: client.info.clusterName });
58
return client.callMany<SysConatServerCallMany>(subject, opts);
59
}
60
61