Skip to content

Commit e2b9aac

Browse files
author
Artem
committed
add connection retry on create + wait until "end" to determine that connection was lost
1 parent eff999f commit e2b9aac

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

redisinsight/api/config/default.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default {
7777
redis_clients: {
7878
idleSyncInterval: parseInt(process.env.CLIENTS_IDLE_SYNC_INTERVAL, 10) || 1000 * 60 * 60, // 1hr
7979
maxIdleThreshold: parseInt(process.env.CLIENTS_MAX_IDLE_THRESHOLD, 10) || 1000 * 60 * 60, // 1hr
80-
retryTimes: parseInt(process.env.CLIENTS_RETRY_TIMES, 10) || 5,
80+
retryTimes: parseInt(process.env.CLIENTS_RETRY_TIMES, 10) || 3,
8181
retryDelay: parseInt(process.env.CLIENTS_RETRY_DELAY, 10) || 500,
8282
maxRetriesPerRequest: parseInt(process.env.CLIENTS_MAX_RETRIES_PER_REQUEST, 10) || 1,
8383
},

redisinsight/api/src/constants/error-messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export default {
1616
CONNECTION_TIMEOUT:
1717
'The connection has timed out, please check the connection details.',
1818
SERVER_CLOSED_CONNECTION: 'Server closed the connection.',
19+
UNABLE_TO_ESTABLISH_CONNECTION: 'Unable to establish connection.',
20+
RECONNECTING_TO_DATABASE: 'Reconnecting to the redis database.',
1921
AUTHENTICATION_FAILED: () => 'Failed to authenticate, please check the username or password.',
2022
INCORRECT_DATABASE_URL: (url) => `Could not connect to ${url}, please check the connection details.`,
2123
INCORRECT_CERTIFICATES: (url) => `Could not connect to ${url}, please check the CA or Client certificate.`,

redisinsight/api/src/modules/database/providers/database.factory.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class DatabaseFactory {
3838
context: ClientContext.Common,
3939
},
4040
database,
41-
{ useRetry: false },
41+
{ useRetry: true },
4242
);
4343

4444
if (await this.databaseInfoProvider.isSentinel(client)) {
@@ -109,7 +109,7 @@ export class DatabaseFactory {
109109
context: ClientContext.Common,
110110
},
111111
model,
112-
{ useRetry: false },
112+
{ useRetry: true },
113113
);
114114

115115
// todo: rethink
@@ -157,7 +157,7 @@ export class DatabaseFactory {
157157
context: ClientContext.Common,
158158
},
159159
model,
160-
{ useRetry: false },
160+
{ useRetry: true },
161161
);
162162

163163
model.connectionType = ConnectionType.SENTINEL;

redisinsight/api/src/modules/redis/redis-connection.factory.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ export class RedisConnectionFactory {
174174

175175
return await new Promise((resolve, reject) => {
176176
try {
177+
let lastError: Error;
178+
177179
if (tnl) {
178180
tnl.on('error', (error) => {
179181
reject(error);
@@ -194,18 +196,20 @@ export class RedisConnectionFactory {
194196
});
195197
connection.on('error', (e): void => {
196198
this.logger.error('Failed connection to the redis database.', e);
197-
reject(e);
199+
lastError = e;
198200
});
199201
connection.on('end', (): void => {
200-
this.logger.error(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION);
201-
reject(new InternalServerErrorException(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION));
202+
this.logger.error(ERROR_MESSAGES.UNABLE_TO_ESTABLISH_CONNECTION, lastError);
203+
reject(lastError || new InternalServerErrorException(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION));
202204
});
203205
connection.on('ready', (): void => {
206+
lastError = null;
204207
this.logger.log('Successfully connected to the redis database');
205208
resolve(connection);
206209
});
207210
connection.on('reconnecting', (): void => {
208-
this.logger.log('Reconnecting to the redis database');
211+
lastError = null;
212+
this.logger.log(ERROR_MESSAGES.RECONNECTING_TO_DATABASE);
209213
});
210214
} catch (e) {
211215
reject(e);
@@ -236,6 +240,8 @@ export class RedisConnectionFactory {
236240

237241
return new Promise((resolve, reject) => {
238242
try {
243+
let lastError: Error;
244+
239245
const cluster = new Cluster([{
240246
host: database.host,
241247
port: database.port,
@@ -244,16 +250,21 @@ export class RedisConnectionFactory {
244250
});
245251
cluster.on('error', (e): void => {
246252
this.logger.error('Failed connection to the redis oss cluster', e);
247-
reject(!isEmpty(e.lastNodeError) ? e.lastNodeError : e);
253+
lastError = !isEmpty(e.lastNodeError) ? e.lastNodeError : e;
248254
});
249255
cluster.on('end', (): void => {
250-
this.logger.error(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION);
251-
reject(new InternalServerErrorException(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION));
256+
this.logger.error(ERROR_MESSAGES.UNABLE_TO_ESTABLISH_CONNECTION, lastError);
257+
reject(lastError || new InternalServerErrorException(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION));
252258
});
253259
cluster.on('ready', (): void => {
260+
lastError = null;
254261
this.logger.log('Successfully connected to the redis oss cluster.');
255262
resolve(cluster);
256263
});
264+
cluster.on('reconnecting', (): void => {
265+
lastError = null;
266+
this.logger.log(ERROR_MESSAGES.RECONNECTING_TO_DATABASE);
267+
});
257268
} catch (e) {
258269
reject(e);
259270
}
@@ -275,19 +286,26 @@ export class RedisConnectionFactory {
275286

276287
return new Promise((resolve, reject) => {
277288
try {
289+
let lastError: Error;
290+
278291
const client = new Redis(config);
279292
client.on('error', (e): void => {
280293
this.logger.error('Failed connection to the redis oss sentinel', e);
281-
reject(e);
294+
lastError = e;
282295
});
283296
client.on('end', (): void => {
284-
this.logger.error(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION);
285-
reject(new InternalServerErrorException(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION));
297+
this.logger.error(ERROR_MESSAGES.UNABLE_TO_ESTABLISH_CONNECTION, lastError);
298+
reject(lastError || new InternalServerErrorException(ERROR_MESSAGES.SERVER_CLOSED_CONNECTION));
286299
});
287300
client.on('ready', (): void => {
301+
lastError = null;
288302
this.logger.log('Successfully connected to the redis oss sentinel.');
289303
resolve(client);
290304
});
305+
client.on('reconnecting', (): void => {
306+
lastError = null;
307+
this.logger.log(ERROR_MESSAGES.RECONNECTING_TO_DATABASE);
308+
});
291309
} catch (e) {
292310
reject(e);
293311
}

0 commit comments

Comments
 (0)