Skip to content

Conversation

@WenZu-Zhou
Copy link
Contributor

无法重新打开PR,只能重新开一个了。

// 1s 内允许 3000 个请求
}

func NewRedisSlidingWindowLimiter(cmd redis.Cmdable,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有个问题,这个文件是在 internal 目录下,因此该目录下的所有结构体,所有方法等,对于使用这个 ginx 库的第三方来说,是不可见的。所以 NewRedisSlidingWindowLimiter 应该放到 internal 的外面,才能让第三方使用。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把这个方法挪过去 middleware/ratelimit 里面。暂时我还不想把 limiter 相关的内容暴露出去。

return func(ctx *gin.Context) {
limited, err := b.limit(ctx)
if err != nil {
log.Println(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你增加一个 logFunc,默认情况下就是用这个 log.Println。

return
}
if limited {
log.Println(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这边不需要打印

}
}

func (b *Builder) SetKey(fn func(*gin.Context) string) *Builder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改名字叫做 KeyGenFunc,或者 SetKeyGenFunc。

genKeyFn func(*gin.Context) string
}

func NewBuilder(limiter ratelimit.Limiter) *Builder {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加上注释,默认情况下使用 IP 限流

// 1s 内允许 3000 个请求
}

func NewRedisSlidingWindowLimiter(cmd redis.Cmdable,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把这个方法挪过去 middleware/ratelimit 里面。暂时我还不想把 limiter 相关的内容暴露出去。

@WenZu-Zhou
Copy link
Contributor Author

已根据建议再次修改

type Builder struct {
limiter ratelimit.Limiter
genKeyFn func(ctx *gin.Context) string
logFn func(msg any, args ...any)
Copy link
Contributor

@chenmingyong0423 chenmingyong0423 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logFn,入参我感觉 err error 就够了,因为你这个 logFn 只用在了打印 error 的场景下。如果是考虑后面需要,那应该是提供另一个 builder 吧,而不是修改原有的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logFn,入参我感觉 err error 就够了,因为你这个 logFn 只用在了打印 error 的场景下。如果是考虑后面需要,那应该是提供另一个 builder 吧,而不是修改原有的。

大明哥的意思是,先适配一下zap,后续有可能弄成 Logger API 的形式。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,暂时先这样。后面看情况要不要扩展。这边之所以加 msg,是我担心如果只打印 error 的话,可能用户看不懂。所以我可以考虑提供更加丰富的错误信息。

type Builder struct {
limiter ratelimit.Limiter
genKeyFn func(ctx *gin.Context) string
logFn func(msg any, args ...any)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,暂时先这样。后面看情况要不要扩展。这边之所以加 msg,是我担心如果只打印 error 的话,可能用户看不懂。所以我可以考虑提供更加丰富的错误信息。

@flycash flycash merged commit c7e1e5e into ecodeclub:main Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants