diff --git a/lib/config.js b/lib/config.js index f1b9d3933..ed257b8e2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -152,20 +152,32 @@ Config.prototype.authenticate = function(user, password) { module.exports = Config var parse_interval_table = { - '': 1, + '': 1000, ms: 1, s: 1000, m: 60*1000, h: 60*60*1000, d: 86400000, + w: 7*86400000, M: 30*86400000, - y: 365.25*30*86400000, + y: 365*86400000, } module.exports.parse_interval = function(interval) { - if (typeof(interval) === 'number') return interval - var m = interval.match(/^((0|[1-9][0-9]*)(\.[0-9]+)?)(ms|s|m|h|d|M|y|)$/) - if (!m) throw new Error('invalid interval: ' + interval) - return Number(m[1]) * parse_interval_table[m[4]] + if (typeof(interval) === 'number') return interval * 1000 + + var result = 0 + var last_suffix = Infinity + interval.split(/\s+/).forEach(function(x) { + var m = x.match(/^((0|[1-9][0-9]*)(\.[0-9]+)?)(ms|s|m|h|d|w|M|y|)$/) + if (!m + || parse_interval_table[m[4]] >= last_suffix + || (m[4] === '' && last_suffix !== Infinity)) { + throw new Error('invalid interval: ' + interval) + } + last_suffix = parse_interval_table[m[4]] + result += Number(m[1]) * parse_interval_table[m[4]] + }) + return result } diff --git a/lib/up-storage.js b/lib/up-storage.js index 9cebfbd61..3d6b5f47d 100644 --- a/lib/up-storage.js +++ b/lib/up-storage.js @@ -35,6 +35,9 @@ function Storage(config, mainconfig) { this.config.url = this.config.url.replace(/\/$/, '') this.maxage = parse_interval(this.config.maxage || '2m') + if (Number(this.config.timeout) >= 1000) { + this.logger.warn('Too big timeout value: ' + this.config.timeout + '\nWe changed time format to nginx-like one\n(see http://wiki.nginx.org/ConfigNotation)\nso please update your config accordingly') + } this.timeout = parse_interval(this.config.timeout || '30s') this.max_fails = Number(this.config.max_fails) || 2 this.fail_timeout = parse_interval(this.config.fail_timeout || '5m') diff --git a/test/unit/parse_interval.js b/test/unit/parse_interval.js index aa583cfb0..b0a0c2dd5 100644 --- a/test/unit/parse_interval.js +++ b/test/unit/parse_interval.js @@ -4,18 +4,30 @@ var assert = require('assert') describe('Parse interval', function() { function add_test(str, res) { it('parse ' + str, function() { - assert.strictEqual(parse_interval(str), res) + if (res === null) { + assert.throws(function() { + console.log(parse_interval(str)) + }) + } else { + assert.strictEqual(parse_interval(str), res) + } }) } - add_test(12345, 12345) - add_test('1000', 1000) + add_test(12345, 12345000) + add_test('1000', 1000000) add_test('1.5s', 1500) add_test('25ms', 25) add_test('2m', 2*1000*60) add_test('3h', 3*1000*60*60) add_test('0.5d', 0.5*1000*60*60*24) + add_test('0.5w', 0.5*1000*60*60*24*7) add_test('1M', 1000*60*60*24*30) - add_test('1y', 1000*60*60*24*30*365.25) + add_test('5s 20ms', 5020) + add_test('1y', 1000*60*60*24*365) + add_test('1y 5', null) + add_test('1m 1m', null) + add_test('1m 1y', null) + add_test('1y 1M 1w 1d 1h 1m 1s 1ms', 34822861001) })