Class: Mongo::Retryable::TokenBucket Private
Do not use. This class is for internal use only.
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/mongo/retryable/token_bucket.rb |
Overview
A thread-safe token bucket for rate limiting retries during server overload. Used by the adaptive retry mechanism.
Class Method Summary
-
.new(capacity: Backpressure::DEFAULT_RETRY_TOKEN_CAPACITY) ⇒ TokenBucket
constructor
Internal use only
Create a new token bucket.
Instance Attribute Summary
- #capacity ⇒ Float readonly Internal use only
Instance Method Summary
-
#consume(count = 1) ⇒ true | false
Internal use only
Consume tokens from the bucket.
-
#deposit(count) ⇒ Float
Internal use only
Deposit tokens into the bucket, up to the maximum capacity.
-
#tokens ⇒ Float
Internal use only
Return the current number of tokens.
Instance Attribute Details
#capacity ⇒ Float (readonly)
# File 'lib/mongo/retryable/token_bucket.rb', line 21
attr_reader :capacity
Instance Method Details
#consume(count = 1) ⇒ true | false
Consume tokens from the bucket.
# File 'lib/mongo/retryable/token_bucket.rb', line 36
def consume(count = 1) @mutex.synchronize do if @tokens >= count @tokens -= count true else false end end end
#deposit(count) ⇒ Float
Deposit tokens into the bucket, up to the maximum capacity.
# File 'lib/mongo/retryable/token_bucket.rb', line 52
def deposit(count) @mutex.synchronize do @tokens = [ @capacity, @tokens + count ].min end end
#tokens ⇒ Float
Return the current number of tokens.
# File 'lib/mongo/retryable/token_bucket.rb', line 26
def tokens @mutex.synchronize { @tokens } end