Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/conat/socket/util.ts
5722 views
1
export const SOCKET_HEADER_CMD = "CN-SocketCmd";
2
export const SOCKET_HEADER_SEQ = "CN-SocketSeq";
3
4
export type State = "disconnected" | "connecting" | "ready" | "closed";
5
6
export type Role = "client" | "server";
7
8
// client pings server this frequently and disconnects if
9
// doesn't get a pong back. Server disconnects client if
10
// it doesn't get a ping as well. This is NOT the primary
11
// keep alive/disconnect mechanism -- it's just a backup.
12
// Primarily we watch the connect/disconnect events from
13
// socketio and use those to manage things. This ping
14
// is entirely a "just in case" backup if some event
15
// were missed (e.g., a kill -9'd process...)
16
export const PING_PONG_INTERVAL = 45000;
17
18
// We queue up unsent writes, but only up to a point (to not have a huge memory issue).
19
// Any write beyond this size result in an exception.
20
// NOTE: in nodejs the default for exactly this is "infinite=use up all RAM", so
21
// maybe we should make this even larger (?).
22
// Also note that this is just the *number* of messages, and a message can have
23
// any size.
24
export const DEFAULT_MAX_QUEUE_SIZE = 1000;
25
26
export let DEFAULT_COMMAND_TIMEOUT = 10_000;
27
export let DEFAULT_KEEP_ALIVE = 25_000;
28
export let DEFAULT_KEEP_ALIVE_TIMEOUT = 10_000;
29
30
export function setDefaultSocketTimeouts({
31
command = DEFAULT_COMMAND_TIMEOUT,
32
keepAlive = DEFAULT_KEEP_ALIVE,
33
keepAliveTimeout = DEFAULT_KEEP_ALIVE_TIMEOUT,
34
}: {
35
command?: number;
36
keepAlive?: number;
37
keepAliveTimeout?: number;
38
}) {
39
DEFAULT_COMMAND_TIMEOUT = command;
40
DEFAULT_KEEP_ALIVE = keepAlive;
41
DEFAULT_KEEP_ALIVE_TIMEOUT = keepAliveTimeout;
42
}
43
44
export type Command = "connect" | "close" | "ping" | "socket";
45
46
import { type Client } from "@cocalc/conat/core/client";
47
48
export interface SocketConfiguration {
49
maxQueueSize?: number;
50
// (Default: true) Whether reconnection is enabled or not.
51
// If set to false, you need to manually reconnect:
52
reconnection?: boolean;
53
// ping other end of the socket if no data is received for keepAlive ms;
54
// if other side doesn't respond within keepAliveTimeout, then the
55
// connection switches to the 'disconnected' state.
56
keepAlive?: number; // default: DEFAULT_KEEP_ALIVE
57
keepAliveTimeout?: number; // default: DEFAULT_KEEP_ALIVE_TIMEOUT}
58
// desc = optional, purely for admin/user
59
desc?: string;
60
// for a socket client, you can specificy a custom load balancer,
61
// instead of just selecting a random socket server (in case of multiple
62
// socket servers with the same subject). This is used
63
// by the persist server.
64
loadBalancer?: (subject: string) => Promise<string>;
65
}
66
67
export interface ConatSocketOptions extends SocketConfiguration {
68
subject: string;
69
client: Client;
70
role: Role;
71
id: string;
72
}
73
74
export const RECONNECT_DELAY = 500;
75
76
// here subject = `${this.subject}.server.${this.serverId}.${this.id}` is what
77
// comes from the client to start the session, and this function returns
78
// `${this.subject}.client.${this.id}`
79
// which is what the client is listening on
80
export function clientSubject(subject: string) {
81
const segments = subject.split(".");
82
const clientId = segments[segments.length - 1];
83
return segments.slice(0, -3).join(".") + ".client." + clientId;
84
}
85
86
export function serverStatusSubject(subject) {
87
return `${subject}.server.status`;
88
}
89
90