Skip to content

Commit

Permalink
bugfix: add error handle to redis adapter to avoid unexpected exit. (#55
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ngot committed Jun 20, 2019
1 parent 9e7f71d commit 6dbcc54
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

exports.io = {
redis: {
host: '127.0.0.1',
port: 6666,
},
};

exports.keys = '123';
4 changes: 4 additions & 0 deletions test/fixtures/apps/socket.io-test-redis-error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "socket.io-test-redis-error",
"version": "0.0.1"
}
19 changes: 19 additions & 0 deletions test/io.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 6dbcc54

Please sign in to comment.