Skip to content

Commit

Permalink
[balancer] allow injecting custom balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed May 5, 2017
1 parent b3fa31a commit 6856ee1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions apicast/src/balancer.lua
Original file line number Diff line number Diff line change
@@ -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])

Expand Down
20 changes: 5 additions & 15 deletions examples/custom-module/blacklist.lua
Original file line number Diff line number Diff line change
@@ -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' }
Expand Down Expand Up @@ -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)
Expand All @@ -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

0 comments on commit 6856ee1

Please sign in to comment.