From 3dbb1add678502faa8c21060e346e6bab3f70715 Mon Sep 17 00:00:00 2001 From: Damian Fortuna Date: Fri, 11 Jan 2019 22:55:45 -0300 Subject: [PATCH] Support TTL Supports specifying ttl into the bucket configuration. So we can avoid limitless disk consumption in the case of fixed (buckets without refill) buckets --- lib/db.js | 5 +++-- test/db.tests.js | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/db.js b/lib/db.js index d5e96c9..686af03 100644 --- a/lib/db.js +++ b/lib/db.js @@ -39,7 +39,8 @@ function normalizeType(params) { 'per_interval', 'interval', 'size', - 'unlimited' + 'unlimited', + 'ttl' ]); INTERVAL_SHORTCUTS.forEach(ish => { @@ -52,7 +53,7 @@ function normalizeType(params) { type.size = type.per_interval; } - if (type.per_interval) { + if (type.per_interval && !type.ttl) { type.ttl = (type.size * type.interval) / type.per_interval; if (process.env.NODE_ENV !== 'test') { diff --git a/test/db.tests.js b/test/db.tests.js index 0c38c8c..b6935ec 100644 --- a/test/db.tests.js +++ b/test/db.tests.js @@ -34,6 +34,10 @@ const types = { size: 10 } } + }, + ttl_test: { + size: 10, + ttl: 100 } }; @@ -138,6 +142,19 @@ describeForEachConfig((getConfig) => { }); }); + it('should support specifying a ttl in the bucket configuration', function (done) { + const params = { type: 'ttl_test', key: '211.45.66.1'}; + db.take(params, function (err) { + if (err) return done(err); + setTimeout(function () { + db._types[params.type].db.get(params.key, function (err, result) { + assert.isUndefined(result); + done(); + }); + }, 300); + }); + }); + it('should return TRUE with right remaining and reset after filling up the bucket', (done) => { var now = 1425920267; db.take({