Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
交错式加权轮询负载均衡算法 interleaved weighted round robin (iwrr) ,时间复杂度O(1),空间复杂度O(n)
实现思路
1.实现中维护两个队列,分别是当前队列current queue和下一次队列next queue。队列中的每个元素entry都维护weight表示在当前队列中任选中次数权重
2.最初时weight与entry权重相同,每次被选中后都会减少step(使用GCD公约数来调整个周期)
3.当weight为0时表示当前周期内不可调度,将其放在下一次队列中。当当前队列current queue为empty时, 表示当前周期所有元素entry都被按权重选择结束,此时对换当前队列和下一次队列开始新的周期。
4.由于始终重复用链表节点,因此该负载均衡算法在初始化后实现可以保证是zero-copy的。
测试结果
基本测试结果如下。
基准测试结果如下。
其他