-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: QPS Limit middleware * chore: use request-ip to get client ip * feat: frequencyLimit schema
- Loading branch information
Showing
8 changed files
with
144 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ApiRequestProps } from 'type/next'; | ||
import requestIp from 'request-ip'; | ||
import { ERROR_ENUM } from '@fastgpt/global/common/error/errorCode'; | ||
import { authFrequencyLimit } from 'common/system/frequencyLimit/utils'; | ||
import { addSeconds } from 'date-fns'; | ||
|
||
// unit: times/s | ||
// how to use? | ||
// export default NextAPI(useQPSLimit(10), handler); // limit 10 times per second for a ip | ||
export function useQPSLimit(limit: number) { | ||
return async (req: ApiRequestProps) => { | ||
const ip = requestIp.getClientIp(req); | ||
if (!ip) { | ||
return; | ||
} | ||
try { | ||
await authFrequencyLimit({ | ||
eventId: 'ip-qps-limit' + ip, | ||
maxAmount: limit, | ||
expiredTime: addSeconds(new Date(), 1) | ||
}); | ||
} catch (_) { | ||
return Promise.reject(ERROR_ENUM.QPSLimitExceed); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { getMongoModel, Schema } from '../../mongo'; | ||
import type { FrequencyLimitSchemaType } from './type'; | ||
|
||
const FrequencyLimitSchema = new Schema({ | ||
eventId: { | ||
type: String, | ||
required: true | ||
}, | ||
amount: { | ||
type: Number, | ||
default: 0 | ||
}, | ||
expiredTime: { | ||
type: Date, | ||
required: true | ||
} | ||
}); | ||
|
||
try { | ||
FrequencyLimitSchema.index({ eventId: 1 }, { unique: true }); | ||
FrequencyLimitSchema.index({ expiredTime: 1 }, { expireAfterSeconds: 0 }); | ||
} catch (error) {} | ||
|
||
export const MongoFrequencyLimit = getMongoModel<FrequencyLimitSchemaType>( | ||
'frequency_limit', | ||
FrequencyLimitSchema | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type FrequencyLimitSchemaType = { | ||
_id: string; | ||
eventId: string; // 事件ID | ||
amount: number; // 当前数量 | ||
expiredTime: Date; // 什么时候过期,过期则重置 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { AuthFrequencyLimitProps } from '@fastgpt/global/common/frequenctLimit/type'; | ||
import { MongoFrequencyLimit } from './schema'; | ||
import { readFromSecondary } from '../../mongo/utils'; | ||
|
||
export const authFrequencyLimit = async ({ | ||
eventId, | ||
maxAmount, | ||
expiredTime | ||
}: AuthFrequencyLimitProps) => { | ||
try { | ||
// 对应 eventId 的 account+1, 不存在的话,则创建一个 | ||
const result = await MongoFrequencyLimit.findOneAndUpdate( | ||
{ | ||
eventId | ||
}, | ||
{ | ||
$inc: { amount: 1 }, | ||
$setOnInsert: { expiredTime } | ||
}, | ||
{ | ||
upsert: true, | ||
new: true, | ||
...readFromSecondary | ||
} | ||
); | ||
|
||
// 因为始终会返回+1的结果,所以这里不能直接等,需要多一个。 | ||
if (result.amount > maxAmount) { | ||
return Promise.reject(result); | ||
} | ||
} catch (error) {} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.