From 8d58c0365861f14b687529392b227307eb86ae1f Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Wed, 2 Mar 2016 14:53:00 +0100 Subject: [PATCH 1/3] Add lib.parameters --- src/lib/parameters.lua | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/lib/parameters.lua diff --git a/src/lib/parameters.lua b/src/lib/parameters.lua new file mode 100644 index 0000000000..fdeb1c38b5 --- /dev/null +++ b/src/lib/parameters.lua @@ -0,0 +1,72 @@ +-- Use of this source code is governed by the Apache 2.0 license; see COPYING. + +module(..., package.seeall) + +local function values(t) + local ret = {} + for k, v in pairs(t) do ret[v] = true end + return ret +end + +-- Given PARAMETERS, a table of parameters, assert that all of the +-- REQUIRED keys are present, fill in any missing values from OPTIONAL, +-- and error if any unknown keys are found. +-- +-- parameters := { k=v, ... } +-- required := { k, ... } +-- optional := { k=v, ... } +-- k, v := not nil +function parse(parameters, required, optional) + local ret = {} + if parameters == nil then parameters = {} end + required = values(required) + if optional == nil then optional = {} end + for k, _ in pairs(required) do + if parameters[k] == nil then error('missing required option ' .. k) end + end + for k, v in pairs(parameters) do + if not required[k] and optional[k] == nil then + error('unrecognized option ' .. k) + end + ret[k] = v + end + for k, v in pairs(optional) do + if ret[k] == nil then ret[k] = v end + end + return ret +end + +function selftest () + print('selftest: lib.parameters') + local equal = require('core.lib').equal + local function assert_equal(parameters, required, optional, expected) + assert(equal(parse(parameters, required, optional), expected)) + end + local function assert_error(parameters, required, optional) + assert(not pcall(parse, parameters, required, optional)) + end + + local req = {'a', 'b'} + local opt = {c=42, d=43} + + assert_equal({a=1, b=2}, req, opt, {a=1, b=2, c=42, d=43}) + assert_equal({a=1, b=2}, req, {}, {a=1, b=2}) + assert_equal({a=1, b=2, c=30}, req, opt, {a=1, b=2, c=30, d=43}) + assert_equal({a=1, b=2, d=10}, req, opt, {a=1, b=2, c=42, d=10}) + assert_equal({d=10}, {}, opt, {c=42, d=10}) + assert_equal({}, {}, opt, {c=42, d=43}) + assert_equal({d=false}, {}, opt, {c=42, d=false}) + assert_equal({d=nil}, {}, opt, {c=42, d=43}) + assert_equal({a=false, b=2}, req, {}, {a=false, b=2}) + + assert_error({}, req, opt) + assert_error({d=30}, req, opt) + assert_error({a=1}, req, opt) + assert_error({b=1}, req, opt) + assert_error({a=nil, b=2}, req, opt) + assert_error({a=1, b=nil}, req, opt) + assert_error({a=1, b=2, d=10, e=100}, req, opt) + assert_error({a=1, b=2, c=4}, req, {}) + assert_error({a=1, b=2}, {}, {}) + print('selftest: ok') +end From 823fcb9efdc3e230d6c922e08e93b6d789d626e7 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Wed, 2 Mar 2016 15:02:56 +0100 Subject: [PATCH 2/3] Convert rate limiter to use parameters.parse --- src/apps/rate_limiter/rate_limiter.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/apps/rate_limiter/rate_limiter.lua b/src/apps/rate_limiter/rate_limiter.lua index 98c1cd9347..69b6a64baa 100644 --- a/src/apps/rate_limiter/rate_limiter.lua +++ b/src/apps/rate_limiter/rate_limiter.lua @@ -25,10 +25,12 @@ RateLimiter = {} -- Source produces synthetic packets of such size local PACKET_SIZE = 60 +local required = {'rate', 'bucket_capacity'} +local optional = {initial_capacity=false} + function RateLimiter:new (arg) - local conf = arg and config.parse_app_arg(arg) or {} - assert(conf.rate) - assert(conf.bucket_capacity) + local conf = parameters.parse(config.parse_app_arg(arg or {}), + required, optional) conf.initial_capacity = conf.initial_capacity or conf.bucket_capacity local o = { From 6d9c7845465cbe4ec33c8db07c60dbe30488717c Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Wed, 2 Mar 2016 16:18:37 +0100 Subject: [PATCH 3/3] Add missing include --- src/apps/rate_limiter/rate_limiter.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/src/apps/rate_limiter/rate_limiter.lua b/src/apps/rate_limiter/rate_limiter.lua index 69b6a64baa..67a7a4dba2 100644 --- a/src/apps/rate_limiter/rate_limiter.lua +++ b/src/apps/rate_limiter/rate_limiter.lua @@ -7,6 +7,7 @@ local link = require("core.link") local config = require("core.config") local packet = require("core.packet") local timer = require("core.timer") +local parameters = require("lib.parameters") local basic_apps = require("apps.basic.basic_apps") local ffi = require("ffi") local C = ffi.C