Skip to content

Commit

Permalink
Support TTL
Browse files Browse the repository at this point in the history
Supports specifying ttl into the bucket configuration. So we can avoid
limitless disk consumption in the case of fixed (buckets without refill)
buckets
  • Loading branch information
dafortune committed Jan 12, 2019
1 parent 5f6adcc commit 3dbb1ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ function normalizeType(params) {
'per_interval',
'interval',
'size',
'unlimited'
'unlimited',
'ttl'
]);

INTERVAL_SHORTCUTS.forEach(ish => {
Expand All @@ -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') {
Expand Down
17 changes: 17 additions & 0 deletions test/db.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const types = {
size: 10
}
}
},
ttl_test: {
size: 10,
ttl: 100
}
};

Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 3dbb1ad

Please sign in to comment.