Skip to content

Instantly share code, notes, and snippets.

@JakubOboza
Created November 8, 2013 14:01
Show Gist options
  • Save JakubOboza/7371434 to your computer and use it in GitHub Desktop.
Save JakubOboza/7371434 to your computer and use it in GitHub Desktop.
require 'redis'
$redis = Redis.new
GLOBAL = "counter_global"
def local
"counter_sec_#{Time.now.to_i}"
end
Cap = 100
$redis.set(GLOBAL, 0)
def try_fetch(url)
if $redis.get(GLOBAL).to_i <= Cap && $redis.incr(local).to_i <= Cap
perform(url)
else
sleep(1)
try_fetch(url)
end
end
def perform(url)
Thread.new do
$redis.incr(GLOBAL)
print "."
$redis.decr(GLOBAL)
end
end
1.upto(1000) do |i|
try_fetch(i)
end
puts $redis.get(GLOBAL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment