Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
feat(balancer) callback fadd/remove now also pass the host-header
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed May 1, 2020
1 parent 624c956 commit a8e36e6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ Release process:
4. commit and tag the release
5. upload rock to LuaRocks

### 4.2.x unreleased
### x.x.x unreleased

- BREAKING: `getPeer` now returns the host-header value instead of the hostname
that was used to add the address. This is only breaking if a host was added through
`addHost` with an ip-address. In that case `getPeer` will no longer return the
ip-address as the hostname, but will now return `nil`.
- Added: option `useSRVname`, if truthy then `getPeer` will return the name as found
in the SRV record, instead of the hostname as added to the balancer.
- Added: callback return an extra parameter; the host-header for the address added/removed.
- Fix: using the module instance instead of the passed one for dns resolution
in the balancer (only affected testing). See [PR 88](https://github.com/Kong/lua-resty-dns-client/pull/88).

Expand Down
13 changes: 8 additions & 5 deletions spec/balancer/base_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe("[balancer_base]", function()
describe("callbacks", function()

local list
local handler = function(balancer, eventname, address, ip, port, hostname)
local handler = function(balancer, eventname, address, ip, port, hostname, hostheader)
assert(({
added = true,
removed = true,
Expand All @@ -174,7 +174,7 @@ describe("[balancer_base]", function()
assert.is.equal(address.port, port)
end
list[#list + 1] = {
balancer, eventname, address, ip, port, hostname,
balancer, eventname, address, ip, port, hostname, hostheader,
}
end

Expand Down Expand Up @@ -202,7 +202,8 @@ describe("[balancer_base]", function()
assert.is.table(list[2][3])
assert.equal("127.0.0.1", list[2][4])
assert.equal(80, list[2][5])
assert.equal("localhost", list[2][6])
assert.equal("localhost", list[2][6]) -- hostname
assert.equal("localhost", list[2][7]) -- hostheader
end)


Expand All @@ -225,7 +226,8 @@ describe("[balancer_base]", function()
assert.is.table(list[2][3])
assert.equal("127.0.0.1", list[2][4])
assert.equal(80, list[2][5])
assert.equal("localhost", list[2][6])
assert.equal("localhost", list[2][6]) -- hostname
assert.equal("localhost", list[2][7]) -- hostheader

b:removeHost("localhost", 80)
ngx.sleep(0.1)
Expand All @@ -240,7 +242,8 @@ describe("[balancer_base]", function()
assert.equal(list[2][3], list[4][3]) -- same address object as added
assert.equal("127.0.0.1", list[4][4])
assert.equal(80, list[4][5])
assert.equal("localhost", list[4][6])
assert.equal("localhost", list[4][6]) -- hostname
assert.equal("localhost", list[4][7]) -- hostheader
end)

end)
Expand Down
23 changes: 15 additions & 8 deletions src/resty/dns/balancer/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function objAddr:delete()
" (host ", (self.host or EMPTY).hostname, ")")

self.host.balancer:callback("removed", self, self.ip,
self.port, self.host.hostname)
self.port, self.host.hostname, self.hostHeader)
self.host.balancer:removeAddress(self)
self.host = nil
end
Expand Down Expand Up @@ -980,7 +980,7 @@ function objBalancer:addAddress(address)
local list = self.addresses
assert(list[address] == nil, "Can't add address twice")

self:callback("added", address, address.ip, address.port, address.host.hostname)
self:callback("added", address, address.ip, address.port, address.host.hostname, address.hostHeader)

list[#list + 1] = address
self:onAddAddress(address)
Expand Down Expand Up @@ -1275,11 +1275,18 @@ end
--
-- Signature of the callback is for address adding/removing:
--
-- `function(balancer, "added"/"removed", address, ip, port, hostname)`
-- `function(balancer, "added"/"removed", address, ip, port, hostname, hostheader)`
--
-- where `ip` might also
-- be a hostname if the DNS resolution returns another name (usually in
-- SRV records).
-- - `address` is the address object added
-- - `ip` is the IP address for this object, but might also be a hostname if
-- the DNS resolution returns another name (usually in SRV records)
-- - `port` is the port to use
-- - `hostname` is the hostname for which the address was added to the balancer
-- with `addHost` (resolving that name caused the creation of this address)
-- - `hostheader` is the hostheader to be used. This can have 3 values; 1) `nil` if the
-- `hostname` added was an ip-address to begin with, 2) it will be equal to the
-- name in `ip` if there is a named SRV entry, and `useSRVname == true`, 3) otherwise
-- it will be equal to `hostname`
--
-- For health updates the signature is:
--
Expand All @@ -1293,9 +1300,9 @@ end
function objBalancer:setCallback(callback)
assert(type(callback) == "function", "expected a callback function")

self.callback = function(balancer, action, address, ip, port, hostname)
self.callback = function(balancer, action, address, ip, port, hostname, hostheader)
local ok, err = ngx.timer.at(0, function(premature)
callback(balancer, action, address, ip, port, hostname)
callback(balancer, action, address, ip, port, hostname, hostheader)
end)

if not ok then
Expand Down

0 comments on commit a8e36e6

Please sign in to comment.