Class: Ollama::Policies::RateLimit::TokenBucket
- Inherits:
-
Object
- Object
- Ollama::Policies::RateLimit::TokenBucket
- Defined in:
- lib/ollama/policies/rate_limit.rb
Overview
Token bucket implementation
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
-
#last_refill ⇒ Object
readonly
Returns the value of attribute last_refill.
-
#rate ⇒ Object
readonly
Returns the value of attribute rate.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #consume(amount = 1) ⇒ Object
-
#initialize(rate:, capacity:) ⇒ TokenBucket
constructor
A new instance of TokenBucket.
- #time_until_ready(amount = 1) ⇒ Object
Constructor Details
#initialize(rate:, capacity:) ⇒ TokenBucket
Returns a new instance of TokenBucket.
66 67 68 69 70 71 72 |
# File 'lib/ollama/policies/rate_limit.rb', line 66 def initialize(rate:, capacity:) @rate = rate.to_f @capacity = capacity.to_f @tokens = capacity.to_f @last_refill = Time.now.to_f @mutex = Mutex.new end |
Instance Attribute Details
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
64 65 66 |
# File 'lib/ollama/policies/rate_limit.rb', line 64 def capacity @capacity end |
#last_refill ⇒ Object (readonly)
Returns the value of attribute last_refill.
64 65 66 |
# File 'lib/ollama/policies/rate_limit.rb', line 64 def last_refill @last_refill end |
#rate ⇒ Object (readonly)
Returns the value of attribute rate.
64 65 66 |
# File 'lib/ollama/policies/rate_limit.rb', line 64 def rate @rate end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
64 65 66 |
# File 'lib/ollama/policies/rate_limit.rb', line 64 def tokens @tokens end |
Instance Method Details
#available? ⇒ Boolean
86 87 88 89 90 91 |
# File 'lib/ollama/policies/rate_limit.rb', line 86 def available? @mutex.synchronize do refill @tokens end end |
#consume(amount = 1) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ollama/policies/rate_limit.rb', line 74 def consume(amount = 1) @mutex.synchronize do refill if @tokens >= amount @tokens -= amount true else false end end end |
#time_until_ready(amount = 1) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/ollama/policies/rate_limit.rb', line 93 def time_until_ready(amount = 1) @mutex.synchronize do refill return 0 if @tokens >= amount needed = amount - @tokens needed / @rate end end |