Class: BozosBuckets::Bucket
- Inherits:
-
Object
- Object
- BozosBuckets::Bucket
- Defined in:
- lib/bozos_buckets.rb
Overview
Class representing a token bucket
Instance Attribute Summary collapse
-
#last_refilled ⇒ Object
readonly
Returns the value of attribute last_refilled.
-
#max_token_count ⇒ Object
readonly
Returns the value of attribute max_token_count.
-
#refill_rate ⇒ Object
readonly
Returns the value of attribute refill_rate.
Instance Method Summary collapse
-
#current_token_count ⇒ Integer
Returns the current count of available tokens, after refilling the bucket based on elapsed time.
-
#initialize(initial_token_count: 100, refill_rate: 1, max_token_count: 100) ⇒ Object
constructor
Constructs a Bucket instance.
-
#refill_bucket ⇒ Integer
Determines how many seconds have passed since the last time the bucket was used, calculates how many tokens should be added to the bucket, and adds them by updating @current_token_count.
-
#use_tokens(count: 1) ⇒ Boolean
Attempt to use tokens from the bucket.
Constructor Details
#initialize(initial_token_count: 100, refill_rate: 1, max_token_count: 100) ⇒ Object
Constructs a Bucket instance
20 21 22 23 24 25 |
# File 'lib/bozos_buckets.rb', line 20 def initialize(initial_token_count: 100, refill_rate: 1, max_token_count: 100) @current_token_count = initial_token_count @refill_rate = refill_rate @max_token_count = max_token_count || initial_token_count reset_last_refilled end |
Instance Attribute Details
#last_refilled ⇒ Object (readonly)
Returns the value of attribute last_refilled.
8 9 10 |
# File 'lib/bozos_buckets.rb', line 8 def last_refilled @last_refilled end |
#max_token_count ⇒ Object (readonly)
Returns the value of attribute max_token_count.
8 9 10 |
# File 'lib/bozos_buckets.rb', line 8 def max_token_count @max_token_count end |
#refill_rate ⇒ Object (readonly)
Returns the value of attribute refill_rate.
8 9 10 |
# File 'lib/bozos_buckets.rb', line 8 def refill_rate @refill_rate end |
Instance Method Details
#current_token_count ⇒ Integer
Returns the current count of available tokens, after refilling the bucket based on elapsed time. This method replaces the attr_reader to prevent clients from getting a stale value by reading the attr directly without refilling the bucket first
65 66 67 68 |
# File 'lib/bozos_buckets.rb', line 65 def current_token_count refill_bucket @current_token_count end |
#refill_bucket ⇒ Integer
Determines how many seconds have passed since the last time the bucket was used, calculates how many tokens should be added to the bucket, and adds them by updating @current_token_count
50 51 52 53 54 55 56 57 |
# File 'lib/bozos_buckets.rb', line 50 def refill_bucket elapsed_seconds = Time.now.to_i - last_refilled tokens_to_add = (elapsed_seconds * refill_rate).floor reset_last_refilled @current_token_count = [@current_token_count + tokens_to_add, max_token_count].min end |
#use_tokens(count: 1) ⇒ Boolean
Attempt to use tokens from the bucket. If there are sufficient tokens, @current_token_count is decremented by the ‘count` and method returns `true`. If there are not sufficient tokens, method returns false without changing the @current_token_count
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bozos_buckets.rb', line 34 def use_tokens(count: 1) refill_bucket if (@current_token_count - count) >= 0 @current_token_count -= count true else false end end |