-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
connection.ts
432 lines (383 loc) Β· 11.1 KB
/
connection.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import type { Backoff } from "./backoff.ts";
import { exponentialBackoff } from "./backoff.ts";
import { ErrorReplyError, isRetriableError } from "./errors.ts";
import type {
ConnectionEventMap,
ConnectionEventType,
TypedEventTarget,
} from "./events.ts";
import {
kUnstableCreateProtocol,
kUnstablePipeline,
kUnstableReadReply,
kUnstableWriteCommand,
} from "./internal/symbols.ts";
import { Protocol as DenoStreamsProtocol } from "./protocol/deno_streams/mod.ts";
import type { Command, Protocol } from "./protocol/shared/protocol.ts";
import type { RedisReply, RedisValue } from "./protocol/shared/types.ts";
import { delay } from "./deps/std/async.ts";
export interface SendCommandOptions {
/**
* When this option is set, simple or bulk string replies are returned as `Uint8Array` type.
*
* @default false
*/
returnUint8Arrays?: boolean;
/**
* When this option is set, the command is executed directly without queueing.
*
* @default false
*/
inline?: boolean;
}
export interface Connection extends TypedEventTarget<ConnectionEventMap> {
name: string | null;
isClosed: boolean;
isConnected: boolean;
close(): void;
connect(): Promise<void>;
reconnect(): Promise<void>;
sendCommand(
command: string,
args?: Array<RedisValue>,
options?: SendCommandOptions,
): Promise<RedisReply>;
/**
* @private
*/
[kUnstableReadReply](returnsUint8Arrays?: boolean): Promise<RedisReply>;
/**
* @private
*/
[kUnstableWriteCommand](command: Command): Promise<void>;
/**
* @private
*/
[kUnstablePipeline](
commands: Array<Command>,
): Promise<Array<RedisReply | ErrorReplyError>>;
}
export interface RedisConnectionOptions {
tls?: boolean;
/**
* A list of root certificates, implies {@linkcode RedisConnectionOptions.tls}
*/
caCerts?: string[];
db?: number;
password?: string;
username?: string;
name?: string;
/**
* @default 10
*/
maxRetryCount?: number;
backoff?: Backoff;
/**
* When this option is set, a `PING` command is sent every specified number of seconds.
*/
healthCheckInterval?: number;
/**
* @private
*/
[kUnstableCreateProtocol]?: (conn: Deno.Conn) => Protocol;
}
export const kEmptyRedisArgs: Array<RedisValue> = [];
interface PendingCommand {
execute: () => Promise<RedisReply>;
resolve: (reply: RedisReply) => void;
reject: (error: unknown) => void;
}
export class RedisConnection
implements Connection, TypedEventTarget<ConnectionEventMap> {
name: string | null = null;
private maxRetryCount = 10;
private readonly hostname: string;
private readonly port: number | string;
private _isClosed = false;
private _isConnected = false;
private backoff: Backoff;
private commandQueue: PendingCommand[] = [];
#conn!: Deno.Conn;
#protocol!: Protocol;
#eventTarget = new EventTarget();
get isClosed(): boolean {
return this._isClosed;
}
get isConnected(): boolean {
return this._isConnected;
}
get isRetriable(): boolean {
return this.maxRetryCount > 0;
}
constructor(
hostname: string,
port: number | string,
private options: RedisConnectionOptions,
) {
this.hostname = hostname;
this.port = port;
if (options.name) {
this.name = options.name;
}
if (options.maxRetryCount != null) {
this.maxRetryCount = options.maxRetryCount;
}
this.backoff = options.backoff ?? exponentialBackoff();
}
private async authenticate(
username: string | undefined,
password: string,
): Promise<void> {
try {
password && username
? await this.sendCommand("AUTH", [username, password], { inline: true })
: await this.sendCommand("AUTH", [password], { inline: true });
} catch (error) {
if (error instanceof ErrorReplyError) {
const authError = new AuthenticationError("Authentication failed", {
cause: error,
});
this.#dispatchEvent("error", { error: authError });
throw authError;
} else {
this.#dispatchEvent("error", { error });
throw error;
}
}
}
private async selectDb(
db: number | undefined = this.options.db,
): Promise<void> {
if (!db) throw new Error("The database index is undefined.");
await this.sendCommand("SELECT", [db], { inline: true });
}
private enqueueCommand(
command: PendingCommand,
) {
this.commandQueue.push(command);
if (this.commandQueue.length === 1) {
this.processCommandQueue();
}
}
sendCommand(
command: string,
args?: Array<RedisValue>,
options?: SendCommandOptions,
): Promise<RedisReply> {
const execute = () =>
this.#protocol.sendCommand(
command,
args ?? kEmptyRedisArgs,
options?.returnUint8Arrays,
);
if (options?.inline) {
return execute();
}
const { promise, resolve, reject } = Promise.withResolvers<RedisReply>();
this.enqueueCommand({ execute, resolve, reject });
return promise;
}
addEventListener<K extends keyof ConnectionEventMap>(
type: K,
callback: (event: CustomEvent<ConnectionEventMap[K]>) => void,
options?: AddEventListenerOptions | boolean,
): void {
return this.#eventTarget.addEventListener(
type,
callback as (event: Event) => void,
options,
);
}
removeEventListener<K extends keyof ConnectionEventMap>(
type: K,
callback: (event: CustomEvent<ConnectionEventMap[K]>) => void,
options?: EventListenerOptions | boolean,
): void {
return this.#eventTarget.removeEventListener(
type,
callback as (event: Event) => void,
options,
);
}
#dispatchEvent<K extends ConnectionEventType>(
type: K,
detail: ConnectionEventMap[K],
): boolean {
return this.#eventTarget.dispatchEvent(new CustomEvent(type, { detail }));
}
[kUnstableReadReply](returnsUint8Arrays?: boolean): Promise<RedisReply> {
return this.#protocol.readReply(returnsUint8Arrays);
}
[kUnstablePipeline](commands: Array<Command>): Promise<RedisReply[]> {
const { promise, resolve, reject } = Promise.withResolvers<
RedisReply[]
>();
const execute = () => this.#protocol.pipeline(commands);
this.enqueueCommand({ execute, resolve, reject } as PendingCommand);
return promise;
}
[kUnstableWriteCommand](command: Command): Promise<void> {
return this.#protocol.writeCommand(command);
}
/**
* Connect to Redis server
*/
async connect(): Promise<void> {
await this.#connect(0);
}
async #connect(retryCount: number) {
try {
const dialOpts: Deno.ConnectOptions = {
hostname: this.hostname,
port: parsePortLike(this.port),
};
const conn: Deno.Conn = this.options?.tls || this.options?.caCerts != null
? await Deno.connectTls({ ...dialOpts, caCerts: this.options?.caCerts })
: await Deno.connect(dialOpts);
this.#conn = conn;
this.#protocol = this.options?.[kUnstableCreateProtocol]?.(conn) ??
new DenoStreamsProtocol(conn);
this._isClosed = false;
this._isConnected = true;
this.#dispatchEvent("connect", undefined);
try {
if (this.options.password != null) {
await this.authenticate(this.options.username, this.options.password);
}
if (this.options.db) {
await this.selectDb(this.options.db);
}
} catch (error) {
this.#close();
throw error;
}
this.#dispatchEvent("ready", undefined);
this.#enableHealthCheckIfNeeded();
} catch (error) {
if (error instanceof AuthenticationError) {
this.#dispatchEvent("error", { error });
this.#dispatchEvent("end", undefined);
throw (error.cause ?? error);
}
const backoff = this.backoff(retryCount);
retryCount++;
if (retryCount >= this.maxRetryCount) {
this.#dispatchEvent("error", { error: error as Error });
this.#dispatchEvent("end", undefined);
throw error;
}
this.#dispatchEvent("reconnecting", { delay: backoff });
await delay(backoff);
await this.#connect(retryCount);
}
}
close() {
this.#close(false);
}
#close(canReconnect = false) {
const isClosedAlready = this._isClosed;
this._isClosed = true;
this._isConnected = false;
try {
this.#conn!.close();
} catch (error) {
if (!(error instanceof Deno.errors.BadResource)) {
this.#dispatchEvent("error", { error: error as Error });
throw error;
}
} finally {
if (!isClosedAlready) {
this.#dispatchEvent("close", undefined);
if (!canReconnect) {
this.#dispatchEvent("end", undefined);
}
}
}
}
async reconnect(): Promise<void> {
try {
await this.sendCommand("PING");
this._isConnected = true;
} catch (error) {
this.#dispatchEvent("error", { error });
this.#close(true);
await this.connect();
await this.sendCommand("PING");
}
}
private async processCommandQueue() {
const [command] = this.commandQueue;
if (!command) return;
try {
const reply = await command.execute();
command.resolve(reply);
} catch (error) {
if (
!isRetriableError(error) ||
this.isManuallyClosedByUser()
) {
this.#dispatchEvent("error", { error });
return command.reject(error);
}
let backoff = 0;
for (let i = 0; i < this.maxRetryCount; i++) {
// Try to reconnect to the server and retry the command
this.#close(true);
try {
this.#dispatchEvent("reconnecting", { delay: backoff });
await this.connect();
const reply = await command.execute();
return command.resolve(reply);
} catch (error) {
this.#dispatchEvent("error", { error }); // TODO: use `AggregateError`?
backoff = this.backoff(i);
await delay(backoff);
}
}
this.#dispatchEvent("error", { error });
command.reject(error);
} finally {
this.commandQueue.shift();
this.processCommandQueue();
}
}
private isManuallyClosedByUser(): boolean {
return this._isClosed && !this._isConnected;
}
#enableHealthCheckIfNeeded() {
const { healthCheckInterval } = this.options;
if (healthCheckInterval == null) {
return;
}
const ping = async () => {
if (this.isManuallyClosedByUser()) {
return;
}
try {
await this.sendCommand("PING");
this._isConnected = true;
} catch (_error) {
// TODO: notify the user of an error
this._isConnected = false;
} finally {
setTimeout(ping, healthCheckInterval);
}
};
setTimeout(ping, healthCheckInterval);
}
}
class AuthenticationError extends Error {}
function parsePortLike(port: string | number | undefined): number {
let parsedPort: number;
if (typeof port === "string") {
parsedPort = parseInt(port);
} else if (typeof port === "number") {
parsedPort = port;
} else {
parsedPort = 6379;
}
if (!Number.isSafeInteger(parsedPort)) {
throw new Error("Port is invalid");
}
return parsedPort;
}