Module: Datadog::Core::RefillableBucket
- Included in:
- BorrowingTokenBucket, TokenBucket
- Defined in:
- lib/datadog/core/refillable_bucket.rb
Overview
Token-bucket balance shared by rate limiter classes: the balance
refills at rate tokens per second toward max_tokens, and including
classes define the admission policy on top of it. Including classes
call initialize_bucket from their constructors to set up the state.
Instance Attribute Summary collapse
-
#last_refill ⇒ Numeric
readonly
Monotonic time of the last refill, in seconds, read from the process monotonic clock.
-
#max_tokens ⇒ Numeric
readonly
Ceiling the balance refills toward.
-
#rate ⇒ Numeric
readonly
Refill rate, in tokens per second.
Instance Method Summary collapse
-
#available_tokens ⇒ Numeric
The token balance as of the last refill; reads between refills return a stale balance.
-
#balance_at(now) ⇒ Numeric
Returns the balance the bucket holds at
now: the stored balance plusratetokens for every second since the last refill, capped atmax_tokens.
Instance Attribute Details
#last_refill ⇒ Numeric (readonly)
Monotonic time of the last refill, in seconds, read from the process monotonic clock.
22 23 24 |
# File 'lib/datadog/core/refillable_bucket.rb', line 22 def last_refill @last_refill end |
#max_tokens ⇒ Numeric (readonly)
Returns ceiling the balance refills toward.
16 17 18 |
# File 'lib/datadog/core/refillable_bucket.rb', line 16 def max_tokens @max_tokens end |
#rate ⇒ Numeric (readonly)
Returns refill rate, in tokens per second.
13 14 15 |
# File 'lib/datadog/core/refillable_bucket.rb', line 13 def rate @rate end |
Instance Method Details
#available_tokens ⇒ Numeric
Returns the token balance as of the last refill; reads between refills return a stale balance.
26 27 28 |
# File 'lib/datadog/core/refillable_bucket.rb', line 26 def available_tokens @tokens end |
#balance_at(now) ⇒ Numeric
Returns the balance the bucket holds at now: the stored balance
plus rate tokens for every second since the last refill, capped
at max_tokens.
36 37 38 39 40 |
# File 'lib/datadog/core/refillable_bucket.rb', line 36 def balance_at(now) accrued = available_tokens + rate * (now - last_refill) accrued = max_tokens if accrued > max_tokens accrued end |