Skip to content

Commit

Permalink
handle socker disconnect error (#7)
Browse files Browse the repository at this point in the history
* handle socker disconnect error

* 4.6.0
  • Loading branch information
kbarbounakis authored Mar 16, 2024
1 parent 888fc73 commit 7c17b51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ class RedisConnectionPool {
}

create() {
return createClient(this.container && this.container.options);
const client = createClient(this.container && this.container.options);
client.on('error', (err) => {
TraceUtils.error('An error occurred while using redis client.');
TraceUtils.error(err);
// handle socket close error
if (err.message === 'Socket closed unexpectedly') {
TraceUtils.info('Socket closed unexpectedly. Disconnecting...');
// quit client
client.disconnect();
}
});
return client;
}

async destroy(client) {
if (client && client.isOpen) {
await client.quit()
await client.quit();
}
}

Expand Down Expand Up @@ -92,9 +103,14 @@ class RedisCacheStrategy {
* @returns Promise<*>
*/
async add(key, value, absoluteExpiration) {
/**
* @type {import('redis').RedisClient}
*/
let client;
try {
// get client
/**
* @type {import('redis').RedisClient}
*/
client = await this.pool.acquire();
if (client.isOpen === false) { await client.connect(); }
// if absolute expiration is defined
Expand Down Expand Up @@ -135,9 +151,11 @@ class RedisCacheStrategy {
* @returns Promise<*>
*/
async remove(key) {
/**
* @type {import('redis').RedisClient}
*/
let client;
try {
// get client
try {
client = await this.pool.acquire();
if (client.isOpen === false) { await client.connect(); }
// remove item by key
Expand Down Expand Up @@ -177,6 +195,9 @@ class RedisCacheStrategy {
* @returns Promise<*>
*/
async get(key) {
/**
* @type {import('redis').RedisClient}
*/
let client;
try {
// get client
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/redis",
"version": "4.1.1",
"version": "4.6.0",
"description": "Most Web Framework Redis Cache Module",
"main": "index.js",
"types": "index.d.ts",
Expand Down

0 comments on commit 7c17b51

Please sign in to comment.