123456789_123456789_123456789_123456789_123456789_

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.

Since:

  • 2.1.0

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#capacityFloat (readonly)

Returns:

  • (Float)

    The maximum capacity of the bucket.

Since:

  • 2.1.0

[ GitHub ]

  
# 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.

Parameters:

  • count (Float) (defaults to: 1)

    The number of tokens to consume.

Returns:

  • (true | false)

    true if the tokens were consumed, false if there were insufficient tokens.

Since:

  • 2.1.0

[ GitHub ]

  
# 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.

Parameters:

  • count (Float)

    The number of tokens to deposit.

Returns:

  • (Float)

    The new token count.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/retryable/token_bucket.rb', line 52

def deposit(count)
  @mutex.synchronize do
    @tokens = [ @capacity, @tokens + count ].min
  end
end

#tokensFloat

Return the current number of tokens.

Returns:

  • (Float)

    The current token count.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/retryable/token_bucket.rb', line 26

def tokens
  @mutex.synchronize { @tokens }
end