Skip to content

Commit

Permalink
[balancer] resty balancer doesn't fall back to the default 80 port, :…
Browse files Browse the repository at this point in the history
…set_peer allows providing an explicit port
  • Loading branch information
mayorova committed Mar 22, 2018
1 parent 0d90052 commit ab2ff03
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed

- Changed the way `resty.balancer` sets the default port, in case no port is specified for the upstream [PR #662](https://github.com/3scale/apicast/pull/662)

## [3.2.0-beta3] - 2018-03-20

### Fixed
Expand Down
11 changes: 10 additions & 1 deletion gateway/src/apicast/balancer.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
local round_robin = require 'resty.balancer.round_robin'
local resty_url = require 'resty.url'
local empty = {}

local _M = { default_balancer = round_robin.new() }

local function get_default_port(upstream_url)
local url = resty_url.split(upstream_url) or empty
local scheme = url[1] or 'http'
return resty_url.default_port(scheme)
end

function _M.call(_, _, balancer)
balancer = balancer or _M.default_balancer
local host = ngx.var.proxy_host -- NYI: return to lower frame
local peers = balancer:peers(ngx.ctx[host])

local peer, err = balancer:set_peer(peers)
local port = get_default_port(ngx.var.proxy_pass)
local peer, err = balancer:set_peer(peers, port)

if not peer then
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
Expand Down
8 changes: 4 additions & 4 deletions gateway/src/resty/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ local function new_peer(server, port)

return {
address,
tonumber(server.port or port or 80, 10)
tonumber(server.port or port, 10) or nil
}
end

Expand All @@ -41,7 +41,7 @@ local function convert_servers(servers, port)
for i =1, #servers do
local peer = new_peer(servers[i], port)

if peer and #peer == 2 then
if peer then
insert(peers, peer)
else
ngx.log(ngx.INFO, 'skipping peer because it misses address or port')
Expand Down Expand Up @@ -87,7 +87,7 @@ function _M.select_peer(self, peers)
return peer
end

function _M.set_peer(self, peers)
function _M.set_peer(self, peers, default_port)
local balancer = self.balancer

if not balancer then
Expand All @@ -101,7 +101,7 @@ function _M.set_peer(self, peers)
return nil, err or 'no peer found'
end

address, port = peer[1], peer[2]
address, port = peer[1], peer[2] or default_port

if not address or not port then
return nil, 'peer missing address or port'
Expand Down
13 changes: 13 additions & 0 deletions spec/resty/balancer_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ describe('resty.balancer', function()
assert.truthy(ok)
assert.spy(b.balancer.set_current_peer).was.called_with('127.0.0.2', 8091)
end)

it('works with default port', function()
local peers = {
{ '127.0.0.2' }
}
b.balancer.set_current_peer = spy.new(function() return true end)

local ok, err = b:set_peer(peers, 443)

assert.falsy(err)
assert.truthy(ok)
assert.spy(b.balancer.set_current_peer).was.called_with('127.0.0.2', 443)
end)
end)

describe('round-robin balancer', function()
Expand Down

0 comments on commit ab2ff03

Please sign in to comment.