From 20df8625b9836f17d442771af9c9bce91c32d5fd Mon Sep 17 00:00:00 2001 From: dnechay Date: Tue, 3 Sep 2024 18:53:09 +0300 Subject: [PATCH] fix: handle redis client error events --- src/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/index.js b/src/index.js index 9da186e..cc239d8 100644 --- a/src/index.js +++ b/src/index.js @@ -6,6 +6,8 @@ const redis = require('redis'); const Scripty = require('node-redis-scripty'); const { EventEmitter } = require('events'); +const noop = () => {}; + /** * Lock constructor. * @@ -46,6 +48,15 @@ class Lock { this._redisConnection = redis.createClient(options.redis); this._redisSubscriber = redis.createClient(options.redis); } + /** + * Since subscriber client is created implicitly and + * it's being an instance of EventEmitter, + * we have to subscribe on error events in order to not + * cause crashes where this library is used. + * + * https://nodejs.org/api/events.html#error-events + */ + this._redisSubscriber.on('error', noop); // Handler to run LUA scripts. Uses caching if possible this._scripty = new Scripty(this._redisConnection);