Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Least-Connections load-balancing algorithm #26

Closed
lonerwolf opened this issue Oct 20, 2019 · 4 comments
Closed

Support Least-Connections load-balancing algorithm #26

lonerwolf opened this issue Oct 20, 2019 · 4 comments
Assignees
Labels
enhancement New feature or request long term It's going to be a long-term work proposal Proposal for this repo
Milestone

Comments

@lonerwolf
Copy link

RT,连接频繁,导致很多连接都归到一个EPOLL下,其他EPOLL上的连接比较少

@lonerwolf lonerwolf added enhancement New feature or request proposal Proposal for this repo labels Oct 20, 2019
@panjf2000
Copy link
Owner

@lonerwolf
现在是用 round-robin 轮询分配的,怎么会导致连接都分配到一个 poller 里?

@lonerwolf
Copy link
Author

@lonerwolf
现在是用 round-robin 轮询分配的,怎么会导致连接都分配到一个 poller 里?

因为轮询,所有有问题,比如2个EPOLL,进来2个连接,两个都是一个,然后2号EPOLL里的这个连接断开了,他重新连接就到1号上去了,不断累积就不平衡了

@panjf2000 panjf2000 changed the title 何时加个进来连接的选择业务最少的EPOLL线程进行处理? Support Least-Connections load-balancing algorithm Oct 20, 2019
@panjf2000
Copy link
Owner

panjf2000 commented Oct 20, 2019

客户端连接是长连接和短连接混合的时候,Round-Robin 负载均衡的确会出现你说的这种问题,这是算法决定的,所以我建议你复用连接或者用 gnetSO_REUSEPORT 模式,让内核来做负载均衡。

因为 Round-Robin 最好实现也最简洁而且性能表现也比较好,所以当初就选择它作为内置负载算法。关于新增其他负载均衡算法,其他算法像最少连接、一致性 Hash 等,可能需要引入新的参数和额外的逻辑(可能会带来性能损耗),我还在调研,慢慢会加入新的负载算法的。

@panjf2000 panjf2000 added the long term It's going to be a long-term work label Oct 20, 2019
@panjf2000 panjf2000 pinned this issue Oct 22, 2019
@panjf2000 panjf2000 modified the milestones: v2, v1.1 Dec 21, 2019
@luyu6056
Copy link

luyu6056 commented Jan 2, 2020

我建议以fd来分配loop,取得连接fd分配之后,将fd分配到 idx为nfd%svr.subLoopGroup.len()的loop里。
那么我们来复现疑问场景。
loop总数为2,fd为1分配到loop1,fd为2分配到loop0。fd2断开,重新再连,获得的fd还是2,2%2=0,这时候分配到loop0,实现均衡:smile:

@panjf2000 panjf2000 unpinned this issue Mar 31, 2020
@panjf2000 panjf2000 modified the milestones: v1.5, 1.1 Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request long term It's going to be a long-term work proposal Proposal for this repo
Projects
None yet
Development

No branches or pull requests

3 participants