Skip to content

Commit

Permalink
fix: simplify redis config
Browse files Browse the repository at this point in the history
  • Loading branch information
rocwind committed Feb 7, 2022
1 parent 7e1829e commit deefe90
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
40 changes: 19 additions & 21 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,25 @@ const config = {
},
// Config for redis (register module, tryLoginTimes module)
redis: {
default: {
host: process.env.REDIS_HOST || '127.0.0.1',
port: process.env.REDIS_PORT || 6379,
password: process.env.REDIS_PASSWORD,
db: process.env.REDIS_DB || 0,
retry_strategy: function (options) {
if (options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with a individual error
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands with a individual error
return new Error('Retry time exhausted');
}
if (options.times_connected > 10) {
// End reconnecting with built in error
return undefined;
}
// reconnect after
return Math.max(options.attempt * 100, 3000);
},
host: process.env.REDIS_HOST || '127.0.0.1',
port: process.env.REDIS_PORT || 6379,
password: process.env.REDIS_PASSWORD,
db: process.env.REDIS_DB || 0,
retry_strategy: function (options) {
if (options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with a individual error
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands with a individual error
return new Error('Retry time exhausted');
}
if (options.times_connected > 10) {
// End reconnecting with built in error
return undefined;
}
// reconnect after
return Math.max(options.attempt * 100, 3000);
},
},
};
Expand Down
8 changes: 4 additions & 4 deletions core/services/account-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ proto.login = function (account, password) {
.then((users) => {
if (tryLoginTimes > 0) {
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
return client
.get(loginKey)
.then((loginErrorTimes) => {
Expand All @@ -137,7 +137,7 @@ proto.login = function (account, password) {
if (!security.passwordVerifySync(password, users.password)) {
if (tryLoginTimes > 0) {
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
client
.exists(loginKey)
.then((isExists) => {
Expand Down Expand Up @@ -177,7 +177,7 @@ proto.sendRegisterCode = function (email) {
.then(() => {
//将token临时存储到redis
var token = security.randToken(40);
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
return client
.setex(`${REGISTER_CODE}${security.md5(email)}`, EXPIRED, token)
.then(() => {
Expand All @@ -201,7 +201,7 @@ proto.checkRegisterCode = function (email, token) {
})
.then(() => {
var registerKey = `${REGISTER_CODE}${security.md5(email)}`;
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
return client.get(registerKey).then((storageToken) => {
if (_.isEmpty(storageToken)) {
throw new AppError.AppError(`验证码已经失效,请您重新获取`);
Expand Down
6 changes: 3 additions & 3 deletions core/services/client-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ proto.clearUpdateCheckCache = function (deploymentKey, appVersion, label, packag
key: deploymentKey,
});
let redisCacheKey = this.getUpdateCheckCacheKey(deploymentKey, appVersion, label, packageHash);
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
return client
.keys(redisCacheKey)
.then((data) => {
Expand Down Expand Up @@ -56,7 +56,7 @@ proto.updateCheckFromCache = function (
return self.updateCheck(deploymentKey, appVersion, label, packageHash);
}
let redisCacheKey = self.getUpdateCheckCacheKey(deploymentKey, appVersion, label, packageHash);
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
return client
.get(redisCacheKey)
.then((data) => {
Expand Down Expand Up @@ -103,7 +103,7 @@ proto.chosenMan = function (packageId, rollout, clientUniqueId) {
if (rolloutClientUniqueIdCache === false) {
return self.random(rollout);
} else {
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
var redisCacheKey = self.getChosenManCacheKey(packageId, rollout, clientUniqueId);
return client
.get(redisCacheKey)
Expand Down
6 changes: 4 additions & 2 deletions core/services/email-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var proto = (module.exports = function () {
proto.sendMail = function (options) {
return new Promise((resolve, reject) => {
if (!_.get(options, 'to')) {
return reject(new AppError.AppError('to是必传参数'));
reject(new AppError.AppError('to是必传参数'));
return;
}
var smtpConfig = _.get(config, 'smtpConfig');
if (!smtpConfig || !smtpConfig.host) {
Expand All @@ -29,7 +30,8 @@ proto.sendMail = function (options) {
var mailOptions = _.assign(defaultMailOptions, options);
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return reject(error);
reject(error);
return;
}
resolve(info);
});
Expand Down
14 changes: 7 additions & 7 deletions core/utils/factory.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';
var util = require('util');
var redis = require('redis');
var config = require('../config');
var _ = require('lodash');
const util = require('util');
const redis = require('redis');
const config = require('../config');
const _ = require('lodash');

var factory = {};
const factory = {};
module.exports = factory;

factory.getRedisClient = function (name) {
const client = redis.createClient(_.get(config, `redis.${name}`));
factory.getRedisClient = function () {
const client = redis.createClient(_.get(config, 'redis'));
return {
del: util.promisify(client.del).bind(client),
exists: util.promisify(client.exists).bind(client),
Expand Down
4 changes: 2 additions & 2 deletions test/api/users/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('api/users/users.test.js', function () {
var account2 = '522539441@qq.com2';
var storageToken;
before(function (done) {
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
client
.get(registerKey)
.then(function (t) {
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('api/users/users.test.js', function () {
describe('sign up', function (done) {
var storageToken;
before(function (done) {
var client = factory.getRedisClient('default');
var client = factory.getRedisClient();
client
.get(registerKey)
.then(function (t) {
Expand Down

0 comments on commit deefe90

Please sign in to comment.