Skip to content

Commit

Permalink
Merge pull request #710 from 3scale/fix-batching-param-batching-policy
Browse files Browse the repository at this point in the history
3scale batching policy: fix batching period assignment from config
  • Loading branch information
davidor authored May 15, 2018
2 parents 5ec4848 + d2f0379 commit 4e54345
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- OpenTracing support [PR #669](https://github.com/3scale/apicast/pull/669)
- Generate new policy scaffold from the CLI [PR #682](https://github.com/3scale/apicast/pull/682)
- 3scale batcher policy [PR #685](https://github.com/3scale/apicast/pull/685)
- 3scale batcher policy [PR #685](https://github.com/3scale/apicast/pull/685), [PR #710](https://github.com/3scale/apicast/pull/710)

### Changed

Expand Down
8 changes: 7 additions & 1 deletion gateway/src/apicast/policy/3scale_batcher/3scale_batcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function _M.new(config)
self.reports_batcher = ReportsBatcher.new(
ngx.shared.batched_reports, 'batched_reports_locks')

self.batch_reports_seconds = config.batch_reports_seconds or
self.batch_reports_seconds = config.batch_report_seconds or
default_batch_reports_seconds

self.report_timer_on = false
Expand Down Expand Up @@ -65,6 +65,10 @@ end
local function report(_, service_id, backend, reports_batcher)
local reports = reports_batcher:get_all(service_id)

if reports then
ngx.log(ngx.DEBUG, '3scale batcher report timer got ', #reports, ' reports')
end

-- TODO: verify if we should limit the number of reports sent in a sigle req
reporter.report(reports, service_id, backend, reports_batcher)
end
Expand All @@ -85,6 +89,8 @@ local function ensure_report_timer_on(self, service_id, backend)
service_id, backend, self.reports_batcher)

self.report_timer_on = true
ngx.log(ngx.DEBUG, 'scheduled 3scale batcher report timer every ',
self.batch_reports_seconds, ' seconds')
end

self.semaphore_report_timer:post()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function _M:get_all(service_id)
local ok, unlock_err = lock:unlock()
if not ok then
ngx.log(ngx.ERR, 'failed to unlock: ', unlock_err)
return
return cached_reports
end

return cached_reports
Expand Down
16 changes: 16 additions & 0 deletions spec/policy/3scale_batcher/3scale_batcher_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ local configuration = require('apicast.configuration')
local lrucache = require('resty.lrucache')

describe('3scale batcher policy', function()
describe('.new', function()
it('allows to configure the batching period', function()
local test_batching_period = 3
local config = { batch_report_seconds = test_batching_period }
local batcher_policy = ThreescaleBatcher.new(config)

assert.equals(test_batching_period, batcher_policy.batch_reports_seconds)
end)

it('assigns a default of 10s for the batching period', function()
local batcher_policy = ThreescaleBatcher.new({})

assert.equals(10, batcher_policy.batch_reports_seconds)
end)
end)

describe('.access', function()
local service = configuration.parse_service({ id = 42 })
local service_id = service.id
Expand Down
1 change: 1 addition & 0 deletions t/apicast-policy-3scale-batcher.t
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ push $res, "Host: one";
$res
--- no_error_log
[error]
3scale batcher report timer got
=== TEST 4: report batched reports to backend
This test checks that reports are sent correctly to backend. To do that, it performs
Expand Down

0 comments on commit 4e54345

Please sign in to comment.