diff --git a/CHANGELOG.md b/CHANGELOG.md index 8704c9719..8801d81d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed + +- APIcast module `balancer` method now accepts optional balancer [PR #362](https://github.com/3scale/apicast/pull/362) + ## [3.1.0-alpha1] - 2017-05-05 ### Changed diff --git a/apicast/src/balancer.lua b/apicast/src/balancer.lua index 303ec2782..b93485497 100644 --- a/apicast/src/balancer.lua +++ b/apicast/src/balancer.lua @@ -1,9 +1,9 @@ local round_robin = require 'resty.balancer.round_robin' -local _M = { } +local _M = { default_balancer = round_robin.new() } -function _M.call() - local balancer = round_robin.new() +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]) diff --git a/examples/custom-module/blacklist.lua b/examples/custom-module/blacklist.lua index bda6c6738..b22976b0e 100644 --- a/examples/custom-module/blacklist.lua +++ b/examples/custom-module/blacklist.lua @@ -1,6 +1,6 @@ local apicast = require('apicast').new() local iputils = require("resty.iputils") -local default_balancer = require('resty.balancer.round_robin').call +local default_balancer = require('balancer').default_balancer local resty_balancer = require('resty.balancer') local _M = { _VERSION = '0.0' } @@ -33,13 +33,13 @@ function _M.new() }, mt) end -function _M:init() +function _M.init() iputils.enable_lrucache() apicast:init() end local balancer_with_blacklist = resty_balancer.new(function(peers) - local peer, i = default_balancer(peers) + local peer, i = default_balancer.mode(peers) local ip = peer[1] local blacklisted, err = iputils.ip_in_cidrs(ip, blacklist) @@ -53,18 +53,8 @@ local balancer_with_blacklist = resty_balancer.new(function(peers) end end) -function _M:balancer() - local balancer = balancer_with_blacklist - 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) - - 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) - end +function _M.balancer() + return apicast:balancer(balancer_with_blacklist) end return _M