Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
337c7c0f
提交
337c7c0f
authored
5月 22, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
滑动窗口ip限速器
上级
c63111d3
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
86 行增加
和
7 行删除
+86
-7
logging.go
gin/middleware/logging.go
+2
-2
rate_limit.go
gin/middleware/ratelimiter/rate_limit.go
+58
-0
rate_limit_test.go
gin/middleware/ratelimiter/rate_limit_test.go
+21
-0
ctx.go
gin/xctx/ctx.go
+4
-4
ip.go
ips/ip.go
+1
-1
没有找到文件。
gin/middleware/logging.go
浏览文件 @
337c7c0f
...
...
@@ -193,8 +193,8 @@ func Logging(opts ...Option) gin.HandlerFunc {
newWriter
:=
&
bodyLogWriter
{
body
:
&
bytes
.
Buffer
{},
ResponseWriter
:
c
.
Writer
}
c
.
Writer
=
newWriter
ip
:=
ips
.
GetIP
(
c
)
c
.
Set
(
ctxUtil
.
KeyIP
,
ip
)
ip
:=
ips
.
Get
Client
IP
(
c
)
c
.
Set
(
ctxUtil
.
Key
Client
IP
,
ip
)
// processing requests
c
.
Next
()
...
...
gin/middleware/ratelimiter/rate_limit.go
0 → 100644
浏览文件 @
337c7c0f
package
ratelimiter
import
(
"sync"
"time"
)
type
Window
struct
{
Start
time
.
Time
Requests
int
}
type
IPRateLimiter
struct
{
mu
sync
.
Mutex
ips
map
[
string
][]
*
Window
windowSize
time
.
Duration
// 滑动窗口时间长度
maxRequests
int
// 窗口内最大请求数
}
func
NewIPRateLimiter
(
windowSize
time
.
Duration
,
maxRequests
int
)
*
IPRateLimiter
{
return
&
IPRateLimiter
{
ips
:
make
(
map
[
string
][]
*
Window
),
windowSize
:
windowSize
,
maxRequests
:
maxRequests
,
}
}
func
(
rl
*
IPRateLimiter
)
Allow
(
ip
string
)
bool
{
rl
.
mu
.
Lock
()
defer
rl
.
mu
.
Unlock
()
now
:=
time
.
Now
()
cutoff
:=
now
.
Add
(
-
rl
.
windowSize
)
if
windows
,
exists
:=
rl
.
ips
[
ip
];
exists
{
var
validWindows
[]
*
Window
for
_
,
w
:=
range
windows
{
if
w
.
Start
.
After
(
cutoff
)
{
validWindows
=
append
(
validWindows
,
w
)
}
}
rl
.
ips
[
ip
]
=
validWindows
}
total
:=
0
for
_
,
w
:=
range
rl
.
ips
[
ip
]
{
total
+=
w
.
Requests
}
if
total
>=
rl
.
maxRequests
{
return
false
}
if
len
(
rl
.
ips
[
ip
])
==
0
||
now
.
Sub
(
rl
.
ips
[
ip
][
len
(
rl
.
ips
[
ip
])
-
1
]
.
Start
)
>
time
.
Second
{
rl
.
ips
[
ip
]
=
append
(
rl
.
ips
[
ip
],
&
Window
{
Start
:
now
,
Requests
:
1
,
})
}
else
{
rl
.
ips
[
ip
][
len
(
rl
.
ips
[
ip
])
-
1
]
.
Requests
++
}
return
true
}
gin/middleware/ratelimiter/rate_limit_test.go
0 → 100644
浏览文件 @
337c7c0f
package
ratelimiter
import
(
"testing"
"time"
)
import
(
"fmt"
)
func
TestRateLimiter
(
t
*
testing
.
T
)
{
rl
:=
NewIPRateLimiter
(
1
*
time
.
Second
,
5
)
// 每分钟10次限制
ip
:=
"192.168.1.1"
for
i
:=
0
;
i
<
100
;
i
++
{
allowed
:=
rl
.
Allow
(
ip
)
fmt
.
Printf
(
"Request %d: %t
\n
"
,
i
+
1
,
allowed
)
time
.
Sleep
(
100
*
time
.
Millisecond
)
}
}
gin/xctx/ctx.go
浏览文件 @
337c7c0f
...
...
@@ -22,7 +22,7 @@ var (
HeaderXRequestIDKey
=
"X-Request-ID"
KeyTid
=
"tid"
Key
IP
=
"IP
"
Key
ClientIP
=
"client_ip
"
KeyUID
=
"userId"
KeyUType
=
"userType"
KeyCompanyID
=
"companyId"
...
...
@@ -139,10 +139,10 @@ func GetCtxTid(ctx context.Context) string {
return
tid
}
// GetIP get ip from context.Context, the client ip is set in gin.Context
func
GetIP
(
ctx
context
.
Context
)
string
{
// Get
Client
IP get ip from context.Context, the client ip is set in gin.Context
func
Get
Client
IP
(
ctx
context
.
Context
)
string
{
ip
:=
""
ipVal
:=
ctx
.
Value
(
KeyIP
)
ipVal
:=
ctx
.
Value
(
Key
Client
IP
)
if
ipVal
!=
nil
{
if
str
,
ok
:=
ipVal
.
(
string
);
ok
{
ip
=
str
...
...
ips/ip.go
浏览文件 @
337c7c0f
...
...
@@ -12,7 +12,7 @@ import (
)
// get ip of client from context
func
GetIP
(
c
*
gin
.
Context
)
string
{
func
Get
Client
IP
(
c
*
gin
.
Context
)
string
{
ip
:=
c
.
Request
.
Header
.
Get
(
"X-Forwarded-For"
)
if
strings
.
Contains
(
ip
,
"127.0.0.1"
)
||
ip
==
""
{
ip
=
c
.
Request
.
Header
.
Get
(
"X-real-ip"
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论