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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Synopsis

local rr_up = package.loaded.my_rr_up

-- Note that Round Robin picks the first server randomly
local server = rr_up:find()

assert(b.set_current_peer(server))
Expand Down
22 changes: 20 additions & 2 deletions lib/resty/roundrobin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local pairs = pairs
local next = next
local tonumber = tonumber
local setmetatable = setmetatable
local math_random = math.random


local _M = {}
Expand Down Expand Up @@ -46,18 +47,35 @@ local function get_gcd(nodes)
return only_key, gcd, max_weight
end

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

local id = nil
local random_index = math_random(count)

for _ = 1, random_index do
id = next(nodes, id)
end

return id
end


function _M.new(_, nodes)
local newnodes = copy(nodes)
local only_key, gcd, max_weight = get_gcd(newnodes)
local last_id = get_random_node_id(nodes)

local self = {
nodes = newnodes, -- it's safer to copy one
only_key = only_key,
max_weight = max_weight,
gcd = gcd,
cw = max_weight,
last_id = nil,
last_id = last_id,
}
return setmetatable(self, mt)
end
Expand All @@ -68,7 +86,7 @@ function _M.reinit(self, nodes)
self.only_key, self.gcd, self.max_weight = get_gcd(newnodes)

self.nodes = newnodes
self.last_id = nil
self.last_id = get_random_node_id(nodes)
self.cw = self.max_weight
end

Expand Down
56 changes: 53 additions & 3 deletions t/roundrobin.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ __DATA__
--- config
location /t {
content_by_lua_block {
math.randomseed(75098)

local roundrobin = require "resty.roundrobin"

local servers = {
Expand All @@ -53,7 +55,6 @@ GET /t
gcd: 2
id: server1
id: server1
id: server1
id: server2
id: server1
id: server2
Expand All @@ -65,6 +66,7 @@ id: server2
id: server1
id: server2
id: server3
id: server1
--- no_error_log
[error]

Expand All @@ -75,6 +77,8 @@ id: server3
--- config
location /t {
content_by_lua_block {
math.randomseed(75098)

local roundrobin = require "resty.roundrobin"

local servers = {
Expand Down Expand Up @@ -104,8 +108,54 @@ id: server3
--- request
GET /t
--- response_body
server1: 50001
server1: 50000
server3: 16666
server2: 33333
server2: 33334
--- no_error_log
[error]



=== TEST 3: random start
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua_block {
math.randomseed(9975098)

local roundrobin = require "resty.roundrobin"

local servers = {
["server1"] = 1,
["server2"] = 1,
["server3"] = 1,
["server4"] = 1,
}

local rr = roundrobin:new(servers, true)
local id = rr:find()
ngx.say(id)

math.randomseed(11175098)

local new_servers = {
["server1"] = 1,
["server2"] = 1,
["server3"] = 1,
["server4"] = 1,
["server5"] = 1,
["server6"] = 1,
}

rr:reinit(new_servers)
id = rr:find()
ngx.say(id)
}
}
--- request
GET /t
--- response_body
server2
server5
--- no_error_log
[error]