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

feature: Round Robin with optional random start #26

Merged
merged 2 commits into from
Jun 25, 2019

Conversation

ElvinEfendi
Copy link
Contributor

It's sometimes useful to have Round Robin algorithm to start with a random node in the given list instead of always starting with the first one. Imagine you have 50 replicas on Nginx and in each replica 16 workers. That means all 50*16 requests will end up in the first server.

For another real world use case please refer to the thread in kubernetes/ingress-nginx#4023 (comment).

This PR adds an optional flag to Round Robin initializer and if the flag is set it picks last_id at random instead of setting it to `nil.

@ElvinEfendi ElvinEfendi changed the title Round Robin with optional random start feature: Round Robin with optional random start May 26, 2019
Copy link
Member

@doujiang24 doujiang24 left a comment

Choose a reason for hiding this comment

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

Yeah, start randomly should be better, thanks for your work :)

README.markdown Outdated
@@ -126,7 +126,7 @@ Both `resty.chash` and `resty.roundrobin` have the same apis.

new
---
**syntax:** `obj, err = class.new(nodes)`
**syntax:** `obj, err = class.new(nodes, random_start?)`
Copy link
Member

Choose a reason for hiding this comment

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

I think we can always make it start randomly.

@@ -46,18 +47,40 @@ local function get_gcd(nodes)
return only_key, gcd, max_weight
end

local function get_random_node_id(nodes)
local count = 0
Copy link
Member

Choose a reason for hiding this comment

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

style: need four space as indent.

@ElvinEfendi
Copy link
Contributor Author

@doujiang24 thanks for quick review! I addressed your comments.

local function get_random_node_id(nodes)
local count = 0
for _, _ in pairs(nodes) do
count = count + 1
Copy link
Member

Choose a reason for hiding this comment

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

style: need four space too :)

local random_index = math_random(count)

for _ = 1, random_index do
id = next(nodes, id)
Copy link
Member

Choose a reason for hiding this comment

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

ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, I missed them - fixed now

@ElvinEfendi
Copy link
Contributor Author

Is there anything else I can do to get this merged and released?

@ElvinEfendi
Copy link
Contributor Author

@doujiang24 any plan here?

@doujiang24 doujiang24 merged commit 3807cd5 into openresty:master Jun 25, 2019
@ElvinEfendi ElvinEfendi deleted the random-start branch June 25, 2019 02:00
@ElvinEfendi
Copy link
Contributor Author

@doujiang24 when are you planning on releasing this?

@doujiang24
Copy link
Member

@ElvinEfendi
yeah, just kicked a new release: https://github.com/openresty/lua-resty-balancer/releases/tag/v0.03.
thanks for your contribution :)

@ElvinEfendi
Copy link
Contributor Author

@doujiang24 thanks! Does it get automatically published to Luarocks? Or do you suggest using OPM?

@doujiang24
Copy link
Member

Does it get automatically published to Luarocks?

not yet.

Or do you suggest using OPM?

OPM doesn't support this library yet since it only supports pure Lua library now.

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