This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement zthrottle inspired throttling
This will implement a zthrottle inspired throttling that will space out requests of a burst with N seconds in between, ignoring all but the first and the last request of the burst. This will make sure the last push of a burst is never swallowed and always reaches the device while still throttling the burst.
- Loading branch information
1 parent
9cb0062
commit 1ff96fd
Showing
5 changed files
with
97 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
local time = require "util.time"; | ||
|
||
local distance = 0; | ||
local api = {}; | ||
local data = {} | ||
|
||
function api:set_distance(d) | ||
distance = d; | ||
end | ||
|
||
function api:incoming(id, callback) | ||
if not data[id] then data[id] = {}; end | ||
-- directly call callback() if the last call for this id was more than `distance` seconds away | ||
if not data[id]["last_call"] or time.now() > data[id]["last_call"] + distance then | ||
data[id]["last_call"] = time.now(); | ||
if data[id]["timer"] then data[id]["timer"]:stop(); data[id]["timer"] = nil; end | ||
module:log("info", "Calling callback directly"); | ||
callback(); | ||
return "allowed"; | ||
-- use timer to delay second invocation | ||
elseif not data[id]["timer"] then | ||
data[id]["timer"] = module:add_timer(distance - (time.now() - data[id]["last_call"]), function() | ||
data[id]["timer"] = nil; | ||
data[id]["last_call"] = time.now(); | ||
module:log("info", "Calling delayed callback"); | ||
callback(); | ||
end); | ||
return "delayed"; | ||
-- ignore all other invocations until the delayed one fired | ||
else | ||
module:log("debug", "Ignoring incoming call"); | ||
return "ignored"; | ||
end | ||
end | ||
|
||
return api; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters