Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
af1f0a40
authored
Nov 12, 2024
by
licoy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 增加`GLOBAL_API_RATE_LIMIT_ENABLE`与`GLOBAL_WEB_RATE_LIMIT_ENABLE`环境变量,支持是否开启访问速率控制
parent
6d2a5a71
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
2 deletions
+14
-2
common/constants.go
+2
-0
middleware/rate-limit.go
+12
-2
No files found.
common/constants.go
View file @
af1f0a40
...
...
@@ -144,9 +144,11 @@ var (
// All duration's unit is seconds
// Shouldn't larger then RateLimitKeyExpirationDuration
var
(
GlobalApiRateLimitEnable
=
GetEnvOrDefaultBool
(
"GLOBAL_API_RATE_LIMIT_ENABLE"
,
true
)
GlobalApiRateLimitNum
=
GetEnvOrDefault
(
"GLOBAL_API_RATE_LIMIT"
,
180
)
GlobalApiRateLimitDuration
=
int64
(
GetEnvOrDefault
(
"GLOBAL_API_RATE_LIMIT_DURATION"
,
180
))
GlobalWebRateLimitEnable
=
GetEnvOrDefaultBool
(
"GLOBAL_WEB_RATE_LIMIT_ENABLE"
,
true
)
GlobalWebRateLimitNum
=
GetEnvOrDefault
(
"GLOBAL_WEB_RATE_LIMIT"
,
60
)
GlobalWebRateLimitDuration
=
int64
(
GetEnvOrDefault
(
"GLOBAL_WEB_RATE_LIMIT_DURATION"
,
180
))
...
...
middleware/rate-limit.go
View file @
af1f0a40
...
...
@@ -13,6 +13,10 @@ var timeFormat = "2006-01-02T15:04:05.000Z"
var
inMemoryRateLimiter
common
.
InMemoryRateLimiter
var
defNext
=
func
(
c
*
gin
.
Context
)
{
c
.
Next
()
}
func
redisRateLimiter
(
c
*
gin
.
Context
,
maxRequestNum
int
,
duration
int64
,
mark
string
)
{
ctx
:=
context
.
Background
()
rdb
:=
common
.
RDB
...
...
@@ -83,11 +87,17 @@ func rateLimitFactory(maxRequestNum int, duration int64, mark string) func(c *gi
}
func
GlobalWebRateLimit
()
func
(
c
*
gin
.
Context
)
{
return
rateLimitFactory
(
common
.
GlobalWebRateLimitNum
,
common
.
GlobalWebRateLimitDuration
,
"GW"
)
if
common
.
GlobalWebRateLimitEnable
{
return
rateLimitFactory
(
common
.
GlobalWebRateLimitNum
,
common
.
GlobalWebRateLimitDuration
,
"GW"
)
}
return
defNext
}
func
GlobalAPIRateLimit
()
func
(
c
*
gin
.
Context
)
{
return
rateLimitFactory
(
common
.
GlobalApiRateLimitNum
,
common
.
GlobalApiRateLimitDuration
,
"GA"
)
if
common
.
GlobalApiRateLimitEnable
{
return
rateLimitFactory
(
common
.
GlobalApiRateLimitNum
,
common
.
GlobalApiRateLimitDuration
,
"GA"
)
}
return
defNext
}
func
CriticalRateLimit
()
func
(
c
*
gin
.
Context
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment