Skip to content

Instantly share code, notes, and snippets.

/limit.rb Secret

Created January 21, 2016 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/cce6c5d27cd5eddd02e8 to your computer and use it in GitHub Desktop.
Save anonymous/cce6c5d27cd5eddd02e8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
Thread.abort_on_exception = true
class JobQueue
def initialize options = {}
@q = Queue.new
@per_minute = options[:per_minute]
Thread.new do
loop do
before = Time.now
@per_minute.times do
Thread.new(&@q.pop)
end
diff = Time.now-before
sleep [60-diff,0].max
end
end
end
def add(&pr)
@q << pr
end
end
jq = JobQueue.new per_minute:6
loop do
jq.add { puts "I should be executed max. 6 times per minute" }
sleep 0.001
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment