forked from denodrivers/redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.ts
178 lines (160 loc) Β· 4.38 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
import { RedisRawReply, sendCommand } from "./io.ts";
import { BufReader, BufWriter } from "./vendor/https/deno.land/std/io/bufio.ts";
type Closer = Deno.Closer;
export interface Connection {
closer: Closer;
reader: BufReader;
writer: BufWriter;
maxRetryCount: number;
retryInterval: number;
isClosed: boolean;
isConnected: boolean;
close(): void;
connect(): Promise<void>;
reconnect(): Promise<void>;
}
export type RedisConnectionOptions = {
tls?: boolean;
db?: number;
password?: string;
name?: string;
maxRetryCount?: number;
retryInterval?: number;
};
export class RedisConnection implements Connection {
name: string | null = null;
closer!: Closer;
reader!: BufReader;
writer!: BufWriter;
maxRetryCount = 0;
retryInterval = 1200;
private retryCount = 0;
private _isClosed = false;
private _isConnected = false;
private connectThunkified: () => Promise<RedisConnection>;
get isClosed(): boolean {
return this._isClosed;
}
get isConnected(): boolean {
return this._isConnected;
}
constructor(
hostname: string,
port: number | string,
private options: RedisConnectionOptions,
) {
this.connectThunkified = this.thunkifyConnect(hostname, port, options);
}
private thunkifyConnect(
hostname: string,
port: string | number,
options: RedisConnectionOptions,
): () => Promise<RedisConnection> {
return async () => {
const dialOpts: Deno.ConnectOptions = {
hostname,
port: parsePortLike(port),
};
const conn: Deno.Conn = options?.tls
? await Deno.connectTls(dialOpts)
: await Deno.connect(dialOpts);
if (options.name) {
this.name = options.name;
}
if (options.maxRetryCount) {
this.maxRetryCount = options.maxRetryCount;
}
if (options.retryInterval) {
this.retryInterval = options.retryInterval;
}
this.closer = conn;
this.reader = new BufReader(conn);
this.writer = new BufWriter(conn);
this._isClosed = false;
this._isConnected = true;
try {
if (options?.password != null) {
await this.authenticate(options.password);
}
if (options?.db) {
await this.selectDb(options.db);
}
} catch (error) {
this.close();
throw error;
}
return this as RedisConnection;
};
}
private authenticate(password: string): Promise<RedisRawReply> {
return sendCommand(this.writer, this.reader, "AUTH", password);
}
private selectDb(
db: number | undefined = this.options.db,
): Promise<RedisRawReply> {
if (!db) throw new Error("The database index is undefined.");
return sendCommand(this.writer, this.reader, "SELECT", db);
}
/**
* Connect to Redis server
*/
async connect(): Promise<void> {
await this.connectThunkified();
}
close() {
this._isClosed = true;
this._isConnected = false;
try {
this.closer!.close();
} catch (error) {
if (!(error instanceof Deno.errors.BadResource)) throw error;
}
}
async reconnect(): Promise<void> {
if (!this.reader.peek(1)) {
throw new Error("Client is closed.");
}
try {
await sendCommand(this.writer, this.reader, "PING");
this._isConnected = true;
} catch (error) {
this._isConnected = false;
return new Promise((resolve, reject) => {
const interval = setInterval(async () => {
if (this.retryCount > this.maxRetryCount) {
this.close();
clearInterval(interval);
reject(new Error("Could not reconnect"));
}
try {
this.close();
await this.connect();
await sendCommand(this.writer, this.reader, "PING");
this._isConnected = true;
this.retryCount = 0;
clearInterval(interval);
resolve();
} catch (err) {
// retrying
} finally {
this.retryCount++;
}
}, this.retryInterval);
});
}
}
}
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;
}