Skip to content

Commit

Permalink
fix: show real redis server time by TIME command (#20)
Browse files Browse the repository at this point in the history
and allow user disable TIME command by supportTimeCommand = false

closes eggjs/egg#3226
  • Loading branch information
fengmk2 committed Nov 28, 2018
1 parent 5c37776 commit fbfbbfa
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ sudo: false
language: node_js
node_js:
- '8'
- '9'
- '10'
- '11'
install:
- npm i npminstall && npminstall
script:
Expand Down
4 changes: 4 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ exports.redis = {
},
app: true,
agent: false,
// redis client will try to use TIME command to detect client is ready or not
// if your redis server not support TIME command, please set this config to false
// see https://redis.io/commands/time
supportTimeCommand: true,

// Single Redis
// client: {
Expand Down
42 changes: 20 additions & 22 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = app => {
};

let count = 0;

function createClient(config, app) {
let client;

Expand All @@ -19,36 +18,35 @@ function createClient(config, app) {
assert(client.host && client.port && client.password !== undefined && client.db !== undefined,
`[egg-redis] 'host: ${client.host}', 'port: ${client.port}', 'password: ${client.password}', 'db: ${client.db}' are required on config`);
});
app.coreLogger.info('[egg-redis] cluster connecting start');

app.coreLogger.info('[egg-redis] cluster connecting');
client = new Redis.Cluster(config.nodes, config);
client.on('connect', function() {
app.coreLogger.info('[egg-redis] cluster connect success');
});
client.on('error', function(error) {
app.coreLogger.error(error);
});
} else {
assert(config.host && config.port && config.password !== undefined && config.db !== undefined,
`[egg-redis] 'host: ${config.host}', 'port: ${config.port}', 'password: ${config.password}', 'db: ${config.db}' are required on config`);

app.coreLogger.info('[egg-redis] connecting redis://:%s@%s:%s/%s',
config.password, config.host, config.port, config.db);

app.coreLogger.info('[egg-redis] server connecting redis://:***@%s:%s/%s',
config.host, config.port, config.db);
client = new Redis(config);
client.on('connect', function() {
app.coreLogger.info('[egg-redis] connect success on redis://:%s@%s:%s/%s',
config.password, config.host, config.port, config.db);
});
client.on('error', function(error) {
app.coreLogger.error(error);
});
}

client.on('connect', () => {
app.coreLogger.info('[egg-redis] client connect success');
});
client.on('error', err => {
app.coreLogger.error('[egg-redis] client error: %s', err);
app.coreLogger.error(err);
});

app.beforeStart(async () => {
const result = await client.info();
const index = count++;
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, redis currentTime: ${result[0]}`);
if (app.config.redis.supportTimeCommand) {
const serverTimes = await client.time();
// [ '1543378095', '393297' ]
const dateTime = new Date(Number(String(serverTimes[0]) + String(serverTimes[1]).substring(0, 3)));
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, redis server currentTime: ${dateTime}`);
} else {
await client.get('egg-redis-hello');
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, client ready`);
}
});

return client;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"ci": {
"type": "travis",
"version": "8, 9",
"version": "8, 10, 11",
"license": true,
"services": "redis-server"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = app => {
return class HomeController extends app.Controller {
* index() {
const { ctx, app } = this;
yield app.redis.set('foo', 'bar');
ctx.body = yield app.redis.get('foo');
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function(app) {
app.get('/', 'home.index');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

exports.redis = {
client: {
host: '127.0.0.1',
port: 6379,
password: '',
db: '0',
},
agent:true,
supportTimeCommand: false,
};

exports.keys = 'keys';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "redisapp"
}
8 changes: 7 additions & 1 deletion test/fixtures/apps/redisapp/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ exports.redis = {
password: '',
db: '0',
},
agent:true
agent:true,
};

exports.logger = {
coreLogger: {
level: 'INFO',
},
};

exports.keys = 'keys';
23 changes: 20 additions & 3 deletions test/redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ const mm = require('egg-mock');
const request = require('supertest');

describe('test/redis.test.js', () => {

describe('single client', () => {
let app;
before(function* () {
before(async () => {
app = mm.app({
baseDir: 'apps/redisapp',
});
yield app.ready();
await app.ready();
});
after(() => app.close());
afterEach(mm.restore);
Expand All @@ -22,6 +21,24 @@ describe('test/redis.test.js', () => {
.expect(200)
.expect('bar');
});
});

describe('single client supportTimeCommand = false', () => {
let app;
before(async () => {
app = mm.app({
baseDir: 'apps/redisapp-supportTimeCommand-false',
});
await app.ready();
});
after(() => app.close());
afterEach(mm.restore);

it('should query', () => {
return request(app.callback())
.get('/')
.expect(200)
.expect('bar');
});
});
});

0 comments on commit fbfbbfa

Please sign in to comment.