0.0
No commit activity in last 3 years
No release in over 3 years
A ruby implementation of the hierarchical token bucket algorithm. It allows you to define rates on different levels of a hierachie to limit the speed of certain operations.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0
 Project Readme

RateLimit::Htb

This is a Ruby implementation of the hierarchical Token bucket algorithm. It allows you to limit the rate certain operations are executed.

Installation

Add this line to your application's Gemfile:

gem 'rate_limit-htb'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rate_limit-htb

Usage

root = RateLimit::Htb::Bucket.new 1000
child1 = RateLimit::Htb::Bucket.new 300, root
child2 = RateLimit::Htb::Bucket.new 600, root

threads = []

# prints stuff twice as fast as the other two
threads << Thread.new do
  100.times do
    child1.blocking_take 200
    puts "stuff"
  end
end

threads << Thread.new do
  100.times do
    child2.blocking_take 400
    puts "stuff2"
  end
end

# Is called rarely because the other thread needs all the tokens.
threads << Thread.new do
  100.times do
    child2.blocking_take 400
    puts "stuff3"
  end
end

threads.each { |t| t.join }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/HappyKadaver/rate_limit-htb.

License

The gem is available as open source under the terms of the MIT License.