Commit ff6eea1d authored by qinhaitao's avatar qinhaitao

feat: 限流

parent f7119984
......@@ -5,7 +5,7 @@ import { recordErrorLog } from '../../utils/common/logger'
import { resultsModel } from '../../sdk'
import { CODE_TYPES } from '../../errorCode'
import { formatUpdatedDataByProjection, formatUpdateUserProjection } from '../../utils/common/format'
import { dbUpdate } from '../../utils/common/db'
import { dbUpdate } from '../../utils/common/mongodb'
import { USER_DB_NAME } from '../../db'
// 检验参数是否存在
......
/** @format */
import { resultsModel } from '../../sdk'
import { CODE_TYPES } from '../../constants'
import { logFn, recordErrorLog } from '../../utils'
/**
* 限制单位时间接口请求次数
*
* @export
* @param {number} limitCount 限制次数
* @param {number} limitTime 限制的单位时间
* @return {*}
*/
export default function requestCountLimit(limitCount: number, limitTime: number) {
return function (target: Object, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value
descriptor.value = async function (...args: any[]) {
const recordTableName = 'c_test'
const [context, baseInfos = {}, services = [], preCheckData = {}] = args
const { activityId } = context.data
const { openId } = context
const handler = context?.cloud?.dataspace?.context?.handler
const now = Date.now()
try {
const data = await context.cloud.dataspace.executeSql(
`SELECT gmt_modified, expired_time, count FROM ${recordTableName} where activity_id = ? and open_id = ? and handler = ?`,
[activityId, openId, handler]
)
const handlerRecord = data?.[0]
if (!handlerRecord) {
context.cloud.dataspace.executeSql(
`insert into ${recordTableName}(activity_id,open_id,handler,count, expired_time) values(?,?)`,
[activityId, openId, handler, 1, now + limitTime]
)
} else {
if (now <= handlerRecord.expired_time) {
if (handlerRecord?.count < limitCount) {
await context.cloud.dataspace.executeSql(
`update ${recordTableName} set count = ? where activity_id = ? and open_id = ? and handler = ?`,
[handlerRecord?.count + 1, activityId, openId, handler]
)
} else {
return resultsModel.error(CODE_TYPES.SYSTEM_ERROR, '请求过于频繁,请稍后再试~')
}
}
if (now > handlerRecord.expired_time) {
await context.cloud.dataspace.executeSql(
`update ${recordTableName} set count = ? expired_time = ? where activity_id = ? and open_id = ? and handler = ?`,
[1, now + limitTime, activityId, openId, handler]
)
}
}
} catch (error) {
console.log('error', error)
recordErrorLog(context, baseInfos, error.toString(), error.stack)
return resultsModel.error(CODE_TYPES.SYSTEM_ERROR)
}
return method.apply(target, [context, baseInfos, services, preCheckData])
}
}
}
/** @format */
import { resultsModel } from '../../sdk'
import { CODE_TYPES } from '../../constants'
import { logFn, recordErrorLog } from '../../utils'
// 限制请求频率 rate单位 ms
export default function requestRateLimit(rate = 500) {
return function (target: Object, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value
descriptor.value = async function (...args: any[]) {
const recordTableName = 'c_test'
const [context, baseInfos = {}, services = [], preCheckData = {}] = args
const { activityId } = context.data
const { openId } = context
const handler = context?.cloud?.dataspace?.context?.handler
try {
const data = await context.cloud.dataspace.executeSql(
`SELECT gmt_modified FROM ${recordTableName} where activity_id = ? and open_id = ? and handler = ?`,
[activityId, openId, handler]
)
const handlerRecord = data?.[0]
if (!handlerRecord) {
context.cloud.dataspace.executeSql(
`insert into ${recordTableName}(activity_id,open_id,handler,count) values(?,?)`,
[activityId, openId, handler, 1]
)
} else {
const lastRequestTime = new Date(handlerRecord.gmt_modified).getTime()
const now = Date.now()
if (now - lastRequestTime < rate) {
return resultsModel.error(CODE_TYPES.SYSTEM_ERROR, '请求过于频繁,请稍后再试~')
} else {
await context.cloud.dataspace.executeSql(
`update ${recordTableName} set gmt_modified = ? where activity_id = ? and open_id = ? and handler = ?`,
[Date.now(), activityId, openId, handler]
)
}
}
} catch (error) {
console.log('error', error)
recordErrorLog(context, baseInfos, error.toString(), error.stack)
return resultsModel.error(CODE_TYPES.SYSTEM_ERROR)
}
return method.apply(target, [context, baseInfos, services, preCheckData])
}
}
}
......@@ -2,7 +2,7 @@
import { resultsModel } from '../../../sdk'
import { CODE_TYPES } from '../../../errorCode'
import { dbCount } from '../../common/db'
import { dbCount } from '../../common/mongodb'
import { STAT_DB_NAME } from '../../../db'
import { STAT_TYPE } from '../../../constants'
......
......@@ -2,7 +2,7 @@
import { resultsModel } from '../../../sdk'
import { CODE_TYPES } from '../../../errorCode'
import { dbFindOne } from '../../common/db'
import { dbFindOne } from '../../common/mongodb'
import { USER_DB_NAME } from '../../../db'
export default async function checkInviteId(context: IContext<{ activityId: string; inviteId: string }>) {
......
......@@ -2,7 +2,7 @@
import { resultsModel } from '../../../sdk'
import { CODE_TYPES } from '../../../errorCode'
import { dbFindOne } from '../db'
import { dbFindOne } from '../mongodb'
import { JOIN_DB_NAME } from '../../../db'
export default async function checkJoinId(context: IContext<IParams>) {
......
......@@ -3,7 +3,7 @@
import { resultsModel } from '../../../sdk'
import { CODE_TYPES } from '../../../errorCode'
import { DRAW_STATUS, PRIZE_TYPE } from '../../../constants'
import { dbFindOne } from '../db'
import { dbFindOne } from '../mongodb'
import { AWARDS_DB_NAME } from '../../../db'
export default async function checkValidPrize(prizeTypes: number[]) {
......
/** @format */
export async function sqlSelect<T>(context: IContext<IParams>, tableName: string, que) {
return context.cloud.dataspace.executeSql(`SELECT * FROM ${tableName} where id = ? and `, [context.data.id])
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment