Class: Ollama::Policies::RateLimit::TokenBucket

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/policies/rate_limit.rb

Overview

Token bucket implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#capacityObject (readonly)

Returns the value of attribute capacity.



64
65
66
# File 'lib/ollama/policies/rate_limit.rb', line 64

def capacity
  @capacity
end

#last_refillObject (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

#rateObject (readonly)

Returns the value of attribute rate.



64
65
66
# File 'lib/ollama/policies/rate_limit.rb', line 64

def rate
  @rate
end

#tokensObject (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

Returns:

  • (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