aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-01-10 20:23:13 -0500
committerHolden Rohrer <hr@hrhr.dev>2020-01-10 20:23:13 -0500
commit51ba72aca77bc98635f267042a38e54d6093147c (patch)
tree813f32d1b42df7ea466d0a753d3c3c87a0a3a143
parent3b2be804224844088a77b7e7de1ba419f22d09c9 (diff)
added rate limiter in jarvis
-rw-r--r--examples/jarvis.js4
1 files changed, 4 insertions, 0 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js
index 0b4b2fe..93061d7 100644
--- a/examples/jarvis.js
+++ b/examples/jarvis.js
@@ -137,8 +137,10 @@ var search = new Space();
search.adhoc('jarvis'); // The search space is the word jarvis, so whenever that's caught, a relevant function can be called.
var read = new Search(search);
var expire = {};
+var limits = []; // an array of senders within the last 5 seconds to act as a 'rate limiter'
function detectPrompt(send, tiles, locs){ // tries to detect the prompt ('jarvis') and calls respond if found.
+ if (limits.indexOf(send) >= 0) return;
for (let i=0; i<locs.length; i++){
let loc = locs[i];
if (read.has(loc)){
@@ -150,6 +152,8 @@ function detectPrompt(send, tiles, locs){ // tries to detect the prompt ('jarvis
if (results.length > 0) respond(results);
expire[loc] = setTimeout(() => {read.del(loc)}, 30000);
}
+ limits.push(send);
+ setTimeout(() => {limits.splice(limits.indexOf(send))});
}
let response = new Space();