From a8e36e620d52445fbeafdf9b8a094b08f1ae25c2 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Fri, 1 May 2020 12:34:25 +0200 Subject: [PATCH] feat(balancer) callback fadd/remove now also pass the host-header --- README.md | 3 ++- spec/balancer/base_spec.lua | 13 ++++++++----- src/resty/dns/balancer/base.lua | 23 +++++++++++++++-------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ca182e9..b683ec3 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ 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 @@ -59,6 +59,7 @@ Release process: 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). diff --git a/spec/balancer/base_spec.lua b/spec/balancer/base_spec.lua index da0062f..5a22349 100644 --- a/spec/balancer/base_spec.lua +++ b/spec/balancer/base_spec.lua @@ -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, @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/src/resty/dns/balancer/base.lua b/src/resty/dns/balancer/base.lua index 1e8ea30..71eb346 100644 --- a/src/resty/dns/balancer/base.lua +++ b/src/resty/dns/balancer/base.lua @@ -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 @@ -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) @@ -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: -- @@ -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