-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #662 from 3scale/default-port-for-upstream
[balancer] Don't force 80 to be the default if port is not provided
- Loading branch information
Showing
6 changed files
with
101 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,52 @@ | ||
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 | ||
|
||
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 peer, err = balancer:set_peer(peers) | ||
local peer, err = balancer:select_peer(peers) | ||
|
||
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) | ||
return exit_service_unavailable() | ||
end | ||
|
||
local address, port = peer[1], peer[2] | ||
|
||
if not address then | ||
ngx.log(ngx.ERR, 'peer missing address') | ||
return exit_service_unavailable() | ||
end | ||
|
||
if not port then | ||
port = get_default_port(ngx.var.proxy_pass) | ||
end | ||
|
||
local ok | ||
ok, err = balancer.balancer.set_current_peer(address, port) | ||
|
||
if not ok then | ||
ngx.log(ngx.ERR, 'failed to set current backend peer: ', err) | ||
return exit_service_unavailable() | ||
end | ||
|
||
ngx.log(ngx.INFO, 'balancer set peer ', address, ':', port) | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
local apicast_balancer = require 'apicast.balancer' | ||
local resty_balancer = require 'resty.balancer' | ||
|
||
describe('apicast.balancer', function() | ||
|
||
describe('.call', function() | ||
local b = resty_balancer.new(function(peers) return peers[1] end) | ||
b.balancer = { } | ||
|
||
ngx.var = { | ||
proxy_host = 'upstream' | ||
} | ||
|
||
it('sets default port from scheme if no port is specified for peers', function() | ||
b.peers = function() return { { '127.0.0.2' } } end | ||
ngx.var.proxy_pass = 'https://example.com' | ||
|
||
b.balancer.set_current_peer = spy.new(function() return true end) | ||
apicast_balancer:call({},b) | ||
assert.spy(b.balancer.set_current_peer).was.called_with('127.0.0.2', 443) | ||
end) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters