Class: Ollama::Policies::RateLimit
- Defined in:
- lib/ollama/policies/rate_limit.rb
Overview
RateLimit policy - token bucket rate limiter
Defined Under Namespace
Classes: TokenBucket
Instance Attribute Summary collapse
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#burst ⇒ Object
readonly
Returns the value of attribute burst.
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
-
#requests_per_minute ⇒ Object
readonly
Returns the value of attribute requests_per_minute.
-
#requests_per_second ⇒ Object
readonly
Returns the value of attribute requests_per_second.
Instance Method Summary collapse
- #around(request, env, &block) ⇒ Object
-
#initialize(requests_per_second: 10.0, requests_per_minute: 300, burst: 20, hooks: {}) ⇒ RateLimit
constructor
A new instance of RateLimit.
Constructor Details
#initialize(requests_per_second: 10.0, requests_per_minute: 300, burst: 20, hooks: {}) ⇒ RateLimit
Returns a new instance of RateLimit.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ollama/policies/rate_limit.rb', line 15 def initialize( requests_per_second: 10.0, requests_per_minute: 300, burst: 20, hooks: {} ) super() @requests_per_second = requests_per_second @requests_per_minute = requests_per_minute @burst = burst @hooks = hooks # Initialize token buckets @second_bucket = TokenBucket.new( rate: requests_per_second, capacity: [burst, requests_per_second].max ) @minute_bucket = TokenBucket.new( rate: requests_per_minute / 60.0, capacity: [burst * 10, requests_per_minute].max ) end |
Instance Attribute Details
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
9 10 11 |
# File 'lib/ollama/policies/rate_limit.rb', line 9 def bucket @bucket end |
#burst ⇒ Object (readonly)
Returns the value of attribute burst.
9 10 11 |
# File 'lib/ollama/policies/rate_limit.rb', line 9 def burst @burst end |
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
9 10 11 |
# File 'lib/ollama/policies/rate_limit.rb', line 9 def hooks @hooks end |
#requests_per_minute ⇒ Object (readonly)
Returns the value of attribute requests_per_minute.
9 10 11 |
# File 'lib/ollama/policies/rate_limit.rb', line 9 def requests_per_minute @requests_per_minute end |
#requests_per_second ⇒ Object (readonly)
Returns the value of attribute requests_per_second.
9 10 11 |
# File 'lib/ollama/policies/rate_limit.rb', line 9 def requests_per_second @requests_per_second end |
Instance Method Details
#around(request, env, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ollama/policies/rate_limit.rb', line 38 def around(request, env, &block) # Wait for both buckets to have tokens wait_for_tokens(@second_bucket) wait_for_tokens(@minute_bucket) # Consume tokens @second_bucket.consume(1) @minute_bucket.consume(1) block.call(request, env) end |