From 7820ddbaeb390acb921512be17db7aec52679fe7 Mon Sep 17 00:00:00 2001 From: Eric Sigler Date: Sun, 19 Jul 2015 19:59:38 -0700 Subject: [PATCH] [misc] Make rate limiting better for large-scale sets When you have 20+ labels, the rate limiting implementation doesn't work as well. Here's a fix. --- lib/lita/handlers/locker_labels.rb | 22 +++++++++-------- lib/lita/handlers/locker_resources.rb | 26 +++++++++++---------- lita-locker.gemspec | 2 +- spec/lita/handlers/locker_labels_spec.rb | 1 + spec/lita/handlers/locker_resources_spec.rb | 1 + 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/lib/lita/handlers/locker_labels.rb b/lib/lita/handlers/locker_labels.rb index d10fb22..70ef0c9 100644 --- a/lib/lita/handlers/locker_labels.rb +++ b/lib/lita/handlers/locker_labels.rb @@ -52,18 +52,20 @@ class LockerLabels < Handler ) def list(response) - should_rate_limit = false + after 0 do + should_rate_limit = false - Label.list.each_slice(10) do |slice| - if should_rate_limit - sleep 1 - else - should_rate_limit = true - end + Label.list.each_slice(5) do |slice| + if should_rate_limit + sleep 3 + else + should_rate_limit = true + end - slice.each do |n| - l = Label.new(n) - response.reply(unlocked(t('label.desc', name: n, state: l.state.value))) + slice.each do |n| + l = Label.new(n) + response.reply(unlocked(t('label.desc', name: n, state: l.state.value))) + end end end end diff --git a/lib/lita/handlers/locker_resources.rb b/lib/lita/handlers/locker_resources.rb index 6e1f991..e20119c 100644 --- a/lib/lita/handlers/locker_resources.rb +++ b/lib/lita/handlers/locker_resources.rb @@ -44,18 +44,20 @@ class LockerResources < Handler ) def list(response) - should_rate_limit = false - - Resource.list.each_slice(10) do |slice| - if should_rate_limit - sleep 1 - else - should_rate_limit = true - end - - slice.each do |r| - res = Resource.new(r) - response.reply(t('resource.desc', name: r, state: res.state.value)) + after 0 do + should_rate_limit = false + + Resource.list.each_slice(5) do |slice| + if should_rate_limit + sleep 3 + else + should_rate_limit = true + end + + slice.each do |r| + res = Resource.new(r) + response.reply(t('resource.desc', name: r, state: res.state.value)) + end end end end diff --git a/lita-locker.gemspec b/lita-locker.gemspec index 19c9159..9ebdc4f 100644 --- a/lita-locker.gemspec +++ b/lita-locker.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |spec| spec.name = 'lita-locker' - spec.version = '1.0.0' + spec.version = '1.0.1' spec.authors = ['Eric Sigler'] spec.email = ['me@esigler.com'] spec.description = '"lock" and "unlock" arbitrary subjects' diff --git a/spec/lita/handlers/locker_labels_spec.rb b/spec/lita/handlers/locker_labels_spec.rb index 3610281..954bceb 100644 --- a/spec/lita/handlers/locker_labels_spec.rb +++ b/spec/lita/handlers/locker_labels_spec.rb @@ -36,6 +36,7 @@ send_command('locker label create foobar') send_command('locker label create bazbat') send_command('locker label list') + sleep 1 # TODO: HAAAACK. Need after to have a more testable behavior. expect(replies.include?('foobar is unlocked')).to eq(true) expect(replies.include?('bazbat is unlocked')).to eq(true) end diff --git a/spec/lita/handlers/locker_resources_spec.rb b/spec/lita/handlers/locker_resources_spec.rb index 956104b..e858074 100644 --- a/spec/lita/handlers/locker_resources_spec.rb +++ b/spec/lita/handlers/locker_resources_spec.rb @@ -31,6 +31,7 @@ send_command('locker resource create foobar') send_command('locker resource create bazbat') send_command('locker resource list') + sleep 1 # TODO: HAAAAACK. Need after to have a more testable behavior. expect(replies).to include('Resource: foobar, state: unlocked') expect(replies).to include('Resource: bazbat, state: unlocked') end