Skip to content

ActiveRequest 限流 单机 以及 redis 集群限流#7

Merged
flycash merged 9 commits into
ecodeclub:mainfrom
PYxy:main
Oct 2, 2023
Merged

ActiveRequest 限流 单机 以及 redis 集群限流#7
flycash merged 9 commits into
ecodeclub:mainfrom
PYxy:main

Conversation

@PYxy
Copy link
Copy Markdown
Contributor

@PYxy PYxy commented Sep 27, 2023

为了兼容本地以及 redis 的ActiveRequest 限流 使用了一个接口

@PYxy
Copy link
Copy Markdown
Contributor Author

PYxy commented Sep 27, 2023

如果先针对 url 限流 在 LocalLimit 里面增加一个map 之类的结构体 记录某个url 的限流个数 ,但是针对ip 貌似不太友好 。限流的判断就变成了 先看看 是不是超过总限流了,不超过再看具体的限流。这样的思路会不会很奇怪,总限流放在builder 上面 而 个性限流放在limit 里面

Copy link
Copy Markdown
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

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

redis 的实现不需要使用 lua 脚本,可以用一种更加简单的方式。
也就是:

cnt := limiter.Incr()
defer limiter.Decr()
if cnt < threshold {
ctx.Next()
} else {
 ctx.Abort()
}

在本地实现里面也是,直接加就可以。

Comment thread middlewares/activelimit/builder.go Outdated
Copy link
Copy Markdown
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

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

没有必要提早抽象一个 activelimiter,你直接在 middleware 里面写就可以。比较简单,没有必要抽象。

@PYxy
Copy link
Copy Markdown
Contributor Author

PYxy commented Sep 30, 2023

只写了 集成测试

Copy link
Copy Markdown
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

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

你需要提供单元测试,简单测试一下就可以。redis 的测试你可以使用 mock。因为已经有生成的 mock 代码了。你可以在 internal 里面创建一个 mocks 的包,而后把 redis 的Mock丢过去那个包里面。

Comment thread middlewares/activelimit/redislimit/builder.go Outdated
@PYxy
Copy link
Copy Markdown
Contributor Author

PYxy commented Oct 1, 2023

本地的ActiveLimit 需要做单元测试吗?感觉这个的单元测试 跟联合测试一样的.

@flycash
Copy link
Copy Markdown
Contributor

flycash commented Oct 1, 2023

它只需要单元测试,它不和 Redis 之类的打交道。

Copy link
Copy Markdown
Contributor

@flycash flycash left a comment

Choose a reason for hiding this comment

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

因为这里很多个实现都用到了 redis,都需要有 redis 的Mock来完成单元测试。所以,你生成 redis 的mock 生成到 internal/mocks 这个目录下。
而后调整所有的测试,使用这个 Mocks 下的 redis mock。

Comment thread middlewares/ratelimit/redis_slide_window.go Outdated
Comment thread middlewares/activelimit/redislimit/builder.go Outdated
Comment thread middlewares/activelimit/locallimit/builder.go Outdated
Comment thread LICENSE Outdated
@flycash
Copy link
Copy Markdown
Contributor

flycash commented Oct 2, 2023

redis 的 mock,你直接放在 internal/mocks 里面,其它使用 redis 的 mock 的,你也一并改为使用这个。不要重复生成。

@flycash flycash merged commit 98e7ead into ecodeclub:main Oct 2, 2023
@flycash flycash mentioned this pull request Oct 2, 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.

2 participants