From 5d4003a90593667ba1f82d39b903d4607f84cb83 Mon Sep 17 00:00:00 2001 From: ngot Date: Wed, 19 Jun 2019 19:48:58 +1000 Subject: [PATCH] bugfix: add error handle to redis adapter to avoid unexpected exit. --- lib/io.js | 7 ++++++- .../config/config.default.js | 10 ++++++++++ .../socket.io-test-redis-error/package.json | 4 ++++ test/io.test.js | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/apps/socket.io-test-redis-error/config/config.default.js create mode 100644 test/fixtures/apps/socket.io-test-redis-error/package.json diff --git a/lib/io.js b/lib/io.js index 02dba9b..1d3b891 100644 --- a/lib/io.js +++ b/lib/io.js @@ -123,7 +123,12 @@ module.exports = app => { } if (config.redis) { - app.io.adapter(redis(config.redis)); + const adapter = redis(config.redis); + // https://github.com/socketio/socket.io-redis/issues/21 + adapter.prototype.on('error', err => { + app.coreLogger.error(err); + }); + app.io.adapter(adapter); debug('[egg-socket.io] init socket.io-redis ready!'); } diff --git a/test/fixtures/apps/socket.io-test-redis-error/config/config.default.js b/test/fixtures/apps/socket.io-test-redis-error/config/config.default.js new file mode 100644 index 0000000..e834e2b --- /dev/null +++ b/test/fixtures/apps/socket.io-test-redis-error/config/config.default.js @@ -0,0 +1,10 @@ +'use strict'; + +exports.io = { + redis: { + host: '127.0.0.1', + port: 6666, + }, +}; + +exports.keys = '123'; diff --git a/test/fixtures/apps/socket.io-test-redis-error/package.json b/test/fixtures/apps/socket.io-test-redis-error/package.json new file mode 100644 index 0000000..7b13123 --- /dev/null +++ b/test/fixtures/apps/socket.io-test-redis-error/package.json @@ -0,0 +1,4 @@ +{ + "name": "socket.io-test-redis-error", + "version": "0.0.1" +} \ No newline at end of file diff --git a/test/io.test.js b/test/io.test.js index abb4336..c7cd0e9 100644 --- a/test/io.test.js +++ b/test/io.test.js @@ -487,6 +487,25 @@ describe('test/socketio.test.js', () => { }, 500); }); } + + it('redis connection error', done => { + const appName = 'socket.io-test-redis-error'; + const app = mm.cluster({ + baseDir: `apps/${appName}`, + workers: 2, + sticky: true, + }); + app.ready().then(() => { + setTimeout(() => { + app.close() + .then(() => { + const errorLog = getErrorLogContent(appName); + assert(contains(errorLog, 'connect ECONNREFUSED 127.0.0.1:6666') > 0); + }) + .then(done, done); + }, 300); + }); + }); }); describe('namespace', () => {