Skip to content

Commit

Permalink
fixes based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mayorova committed Apr 3, 2018
1 parent ab2ff03 commit c353d01
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### 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)
- `resty.balancer` doesn't fall back to the port `80` by default. If the port is missing, `apicast.balancer` sets the default port for the scheme of the `proxy_pass` URL [PR #662](https://github.com/3scale/apicast/pull/662)

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

Expand Down
35 changes: 30 additions & 5 deletions gateway/src/apicast/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,43 @@ local function get_default_port(upstream_url)
return resty_url.default_port(scheme)
end

local function exit_service_unavailable()
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
ngx.exit(ngx.status)
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 port = get_default_port(ngx.var.proxy_pass)
local peer, err = balancer:set_peer(peers, port)
local peer, err = balancer:select_peer(peers)

local address, port, ok

if not peer then
ngx.status = ngx.HTTP_SERVICE_UNAVAILABLE
ngx.log(ngx.ERR, "failed to set current backend peer: ", err)
ngx.exit(ngx.status)
ngx.log(ngx.ERR, 'could not select peer: ', err)
exit_service_unavailable()
end

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

if not address then
ngx.log(ngx.ERR, 'peer missing address')
exit_service_unavailable()
end

if not port then
port = get_default_port(ngx.var.proxy_pass)
end

ok, err = balancer.balancer.set_current_peer(address, port)

ngx.log(ngx.INFO, 'balancer set peer ', address, ':', port)

if err then
ngx.log(ngx.ERR, 'failed to set current backend peer: ', err)
exit_service_unavailable()
end
end

Expand Down
6 changes: 3 additions & 3 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, 10) or nil
tonumber(server.port or port, 10)
}
end

Expand Down Expand Up @@ -87,7 +87,7 @@ function _M.select_peer(self, peers)
return peer
end

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

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

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

if not address or not port then
return nil, 'peer missing address or port'
Expand Down

0 comments on commit c353d01

Please sign in to comment.