-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathconnection_fallback.ts
197 lines (166 loc) · 6.2 KB
/
connection_fallback.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import axios, { AxiosRequestConfig, CancelTokenSource } from 'axios';
import { StreamChat } from './client';
import { addConnectionEventListeners, removeConnectionEventListeners, retryInterval, sleep } from './utils';
import { isAPIError, isConnectionIDError, isErrorRetryable } from './errors';
import { ConnectionOpen, Event, UR, ExtendableGenerics, DefaultGenerics, LogLevel } from './types';
export enum ConnectionState {
Closed = 'CLOSED',
Connected = 'CONNECTED',
Connecting = 'CONNECTING',
Disconnected = 'DISCONNECTED',
Init = 'INIT',
}
export class WSConnectionFallback<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> {
client: StreamChat<StreamChatGenerics>;
state: ConnectionState;
consecutiveFailures: number;
connectionID?: string;
cancelToken?: CancelTokenSource;
constructor({ client }: { client: StreamChat<StreamChatGenerics> }) {
this.client = client;
this.state = ConnectionState.Init;
this.consecutiveFailures = 0;
addConnectionEventListeners(this._onlineStatusChanged);
}
_log(msg: string, extra: UR = {}, level: LogLevel = 'info') {
this.client.logger(level, 'WSConnectionFallback:' + msg, { tags: ['connection_fallback', 'connection'], ...extra });
}
_setState(state: ConnectionState) {
this._log(`_setState() - ${state}`);
// transition from connecting => connected
if (this.state === ConnectionState.Connecting && state === ConnectionState.Connected) {
this.client.dispatchEvent({ type: 'connection.changed', online: true });
}
if (state === ConnectionState.Closed || state === ConnectionState.Disconnected) {
this.client.dispatchEvent({ type: 'connection.changed', online: false });
}
this.state = state;
}
/** @private */
_onlineStatusChanged = (event: { type: string }) => {
this._log(`_onlineStatusChanged() - ${event.type}`);
if (event.type === 'offline') {
this._setState(ConnectionState.Closed);
this.cancelToken?.cancel('disconnect() is called');
this.cancelToken = undefined;
return;
}
if (event.type === 'online' && this.state === ConnectionState.Closed) {
this.connect(true);
}
};
/** @private */
_req = async <T = UR>(params: UR, config: AxiosRequestConfig, retry: boolean): Promise<T> => {
if (!this.cancelToken && !params.close) {
this.cancelToken = axios.CancelToken.source();
}
try {
const res = await this.client.doAxiosRequest<T>(
'get',
(this.client.baseURL as string).replace(':3030', ':8900') + '/longpoll', // replace port if present for testing with local API
undefined,
{
config: { ...config, cancelToken: this.cancelToken?.token },
params,
},
);
this.consecutiveFailures = 0; // always reset in case of no error
return res;
} catch (err) {
this.consecutiveFailures += 1;
if (retry && isErrorRetryable(err)) {
this._log(`_req() - Retryable error, retrying request`);
await sleep(retryInterval(this.consecutiveFailures));
return this._req<T>(params, config, retry);
}
throw err;
}
};
/** @private */
_poll = async () => {
while (this.state === ConnectionState.Connected) {
try {
const data = await this._req<{
events: Event<StreamChatGenerics>[];
}>({}, { timeout: 30000 }, true); // 30s => API responds in 20s if there is no event
if (data.events?.length) {
for (let i = 0; i < data.events.length; i++) {
this.client.dispatchEvent(data.events[i]);
}
}
} catch (err) {
if (axios.isCancel(err)) {
this._log(`_poll() - axios canceled request`);
return;
}
/** client.doAxiosRequest will take care of TOKEN_EXPIRED error */
if (isConnectionIDError(err)) {
this._log(`_poll() - ConnectionID error, connecting without ID...`);
this._setState(ConnectionState.Disconnected);
this.connect(true);
return;
}
if (isAPIError(err) && !isErrorRetryable(err)) {
this._setState(ConnectionState.Closed);
return;
}
await sleep(retryInterval(this.consecutiveFailures));
}
}
};
/**
* connect try to open a longpoll request
* @param reconnect should be false for first call and true for subsequent calls to keep the connection alive and call recoverState
*/
connect = async (reconnect = false) => {
if (this.state === ConnectionState.Connecting) {
this._log('connect() - connecting already in progress', { reconnect }, 'warn');
return;
}
if (this.state === ConnectionState.Connected) {
this._log('connect() - already connected and polling', { reconnect }, 'warn');
return;
}
this._setState(ConnectionState.Connecting);
this.connectionID = undefined; // connect should be sent with empty connection_id so API creates one
try {
const { event } = await this._req<{ event: ConnectionOpen<StreamChatGenerics> }>(
{ json: this.client._buildWSPayload() },
{ timeout: 8000 }, // 8s
reconnect,
);
this._setState(ConnectionState.Connected);
this.connectionID = event.connection_id;
// @ts-expect-error
this.client.dispatchEvent(event);
this._poll();
if (reconnect) {
this.client.recoverState();
}
return event;
} catch (err) {
this._setState(ConnectionState.Closed);
throw err;
}
};
/**
* isHealthy checks if there is a connectionID and connection is in Connected state
*/
isHealthy = () => {
return !!this.connectionID && this.state === ConnectionState.Connected;
};
disconnect = async (timeout = 2000) => {
removeConnectionEventListeners(this._onlineStatusChanged);
this._setState(ConnectionState.Disconnected);
this.cancelToken?.cancel('disconnect() is called');
this.cancelToken = undefined;
const connection_id = this.connectionID;
this.connectionID = undefined;
try {
await this._req({ close: true, connection_id }, { timeout }, false);
this._log(`disconnect() - Closed connectionID`);
} catch (err) {
this._log(`disconnect() - Failed`, { err }, 'error');
}
};
}