Skip to content

Commit

Permalink
fix: update deps redis to v4, reuse the same redis client without quit
Browse files Browse the repository at this point in the history
  • Loading branch information
rocwind committed Feb 9, 2022
1 parent 78d42d0 commit 98c2ca6
Show file tree
Hide file tree
Showing 27 changed files with 194 additions and 205 deletions.
4 changes: 4 additions & 0 deletions docs/install-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ $ sudo npm i -g pm2

> notice. mysql8.x default auth caching_sha2_pasword not support in node-mysql2 see [issue](https://github.com/mysqljs/mysql/pull/1962)
## Install Redis

- [Redis Quick Start](https://redis.io/topics/quickstart)

## Get code-push-server from NPM

```shell
Expand Down
72 changes: 55 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"qiniu": "7.4.0",
"rand-token": "1.0.1",
"recursive-readdir": "2.2.2",
"redis": "3.1.2",
"redis": "4.0.3",
"sequelize": "6.16.0",
"slash": "3.0.0",
"validator": "13.7.0",
Expand Down
20 changes: 2 additions & 18 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,9 @@ export const config = {
// Config for redis (register module, tryLoginTimes module)
redis: {
host: process.env.REDIS_HOST || '127.0.0.1',
port: process.env.REDIS_PORT || 6379,
port: toNumber(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);
},
db: toNumber(process.env.REDIS_DB, 0),
},
} as const;

Expand Down
71 changes: 27 additions & 44 deletions src/core/services/account-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { UserTokens } from '../../models/user_tokens';
import { findCollaboratorsByAppNameAndUid } from '../../models/collaborators';
import { config } from '../config';
import { AppError } from '../app-error';
import { redisClient } from '../utils/connections';

var security = require('../utils/security');
var factory = require('../utils/factory');
var EmailManager = require('./email-manager');

const LOGIN_LIMIT_PRE = 'LOGIN_LIMIT_PRE_';
Expand Down Expand Up @@ -109,7 +109,7 @@ class AccountManager {
} else {
where = { username: account };
}
var tryLoginTimes = _.get(config, 'common.tryLoginTimes', 0);
const { tryLoginTimes } = config.common;
return Users.findOne({ where: where })
.then((users) => {
if (_.isEmpty(users)) {
Expand All @@ -119,39 +119,29 @@ class AccountManager {
})
.then((users) => {
if (tryLoginTimes > 0) {
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
var client = factory.getRedisClient();
return client
.get(loginKey)
.then((loginErrorTimes) => {
if (loginErrorTimes > tryLoginTimes) {
throw new AppError(`您输入密码错误次数超过限制,帐户已经锁定`);
}
return users;
})
.finally(() => client.quit());
const loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
return redisClient.get(loginKey).then((loginErrorTimes) => {
if (Number(loginErrorTimes) > tryLoginTimes) {
throw new AppError(`您输入密码错误次数超过限制,帐户已经锁定`);
}
return users;
});
} else {
return users;
}
})
.then((users) => {
if (!security.passwordVerifySync(password, users.password)) {
if (tryLoginTimes > 0) {
var loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
var client = factory.getRedisClient();
client
.exists(loginKey)
.then((isExists) => {
if (!isExists) {
var expires = moment().endOf('day').unix() - moment().unix();
return client.setex(loginKey, expires, 0);
}
return isExists;
})
.then(() => {
return client.incr(loginKey);
})
.finally(() => client.quit());
const loginKey = `${LOGIN_LIMIT_PRE}${users.id}`;
redisClient.exists(loginKey).then((isExists) => {
if (!isExists) {
var expires = moment().endOf('day').unix() - moment().unix();
redisClient.setEx(loginKey, expires, '1');
return;
}
redisClient.incr(loginKey);
});
}
throw new AppError('您输入的邮箱或密码有误');
} else {
Expand All @@ -173,13 +163,11 @@ class AccountManager {
.then(() => {
//将token临时存储到redis
var token = security.randToken(40);
var client = factory.getRedisClient();
return client
.setex(`${REGISTER_CODE}${security.md5(email)}`, EXPIRED, token)
return redisClient
.setEx(`${REGISTER_CODE}${security.md5(email)}`, EXPIRED, token)
.then(() => {
return token;
})
.finally(() => client.quit());
});
})
.then((token) => {
//将token发送到用户邮箱
Expand All @@ -197,21 +185,16 @@ class AccountManager {
})
.then(() => {
var registerKey = `${REGISTER_CODE}${security.md5(email)}`;
var client = factory.getRedisClient();
return client.get(registerKey).then((storageToken) => {
return redisClient.get(registerKey).then((storageToken) => {
if (_.isEmpty(storageToken)) {
throw new AppError(`验证码已经失效,请您重新获取`);
}
if (!_.eq(token, storageToken)) {
client
.ttl(registerKey)
.then((ttl) => {
if (ttl > 0) {
return client.expire(registerKey, ttl - EXPIRED_SPEED);
}
return ttl;
})
.finally(() => client.quit());
redisClient.ttl(registerKey).then((ttl) => {
if (ttl > 0) {
redisClient.expire(registerKey, ttl - EXPIRED_SPEED);
}
});
throw new AppError(`您输入的验证码不正确,请重新输入`);
}
return storageToken;
Expand Down
2 changes: 1 addition & 1 deletion src/core/services/app-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Apps } from '../../models/apps';
import { Collaborators } from '../../models/collaborators';
import { Deployments } from '../../models/deployments';
import { Users } from '../../models/users';
import { sequelize } from '../../models/index';
import { sequelize } from '../utils/connections';
import { AppError } from '../app-error';

var security = require('../../core/utils/security');
Expand Down
Loading

0 comments on commit 98c2ca6

Please sign in to comment.