Skip to content

Commit

Permalink
[misc] Make rate limiting better for large-scale sets
Browse files Browse the repository at this point in the history
When you have 20+ labels, the rate limiting implementation doesn't work
as well.  Here's a fix.
  • Loading branch information
esigler committed Jul 20, 2015
1 parent 261ac14 commit 7820ddb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
22 changes: 12 additions & 10 deletions lib/lita/handlers/locker_labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions lib/lita/handlers/locker_resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lita-locker.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ['[email protected]']
spec.description = '"lock" and "unlock" arbitrary subjects'
Expand Down
1 change: 1 addition & 0 deletions spec/lita/handlers/locker_labels_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/lita/handlers/locker_resources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7820ddb

Please sign in to comment.