-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 请求速率限制 * perf: 优化代码 --------- Co-authored-by: ChenZhaoYu <[email protected]>
- Loading branch information
1 parent
2cf7a52
commit 9baf59a
Showing
7 changed files
with
42 additions
and
1 deletion.
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
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 |
---|---|---|
|
@@ -27,3 +27,6 @@ SOCKS_PROXY_PORT= | |
|
||
# HTTPS PROXY | ||
HTTPS_PROXY= | ||
|
||
# Rate Limit | ||
MAX_REQUEST_PER_HOUR= |
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.
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,19 @@ | ||
import { rateLimit } from 'express-rate-limit' | ||
import { isNotEmptyString } from '../utils/is' | ||
|
||
const MAX_REQUEST_PER_HOUR = process.env.MAX_REQUEST_PER_HOUR | ||
|
||
const maxCount = (isNotEmptyString(MAX_REQUEST_PER_HOUR) && !isNaN(Number(MAX_REQUEST_PER_HOUR))) | ||
? parseInt(MAX_REQUEST_PER_HOUR) | ||
: 0 // 0 means unlimited | ||
|
||
const limiter = rateLimit({ | ||
windowMs: 60 * 60 * 1000, // Maximum number of accesses within an hour | ||
max: maxCount, | ||
statusCode: 200, // 200 means success,but the message is 'Too many request from this IP in 1 hour' | ||
message: async (req, res) => { | ||
res.send({ status: 'Fail', message: 'Too many request from this IP in 1 hour', data: null }) | ||
}, | ||
}) | ||
|
||
export { limiter } |