Class: RateLimiter::Bucket
- Inherits:
-
Object
- Object
- RateLimiter::Bucket
- Defined in:
- lib/fluent/plugin/rate_limiter.rb
Overview
Bucket class, contains the rate limiting logic for each group Attributes:
+group+: Group for which the bucket is created
+bucket_count+: Number of requests in the bucket
+bucket_count_total+: Number of requests in the bucket including the dropped requests
+bucket_last_reset+: Time when the bucket was last reset
+approx_rate_per_second+: Approximate rate of requests per second
+rate_last_reset+: Time when the rate was last reset
+curr_count+: Number of requests in the current second
+last_warning+: Time when the last warning was issued
+timeout_s+: Timeout for the bucket
+bucket_limit+: Maximum number of requests allowed in the bucket
+bucket_period+: Time period for the bucket
+rate_limit+: Maximum number of requests allowed per second
Instance Attribute Summary collapse
-
#approx_rate_per_second ⇒ Object
Returns the value of attribute approx_rate_per_second.
-
#bucket_count ⇒ Object
Returns the value of attribute bucket_count.
-
#bucket_count_total ⇒ Object
Returns the value of attribute bucket_count_total.
-
#bucket_last_reset ⇒ Object
Returns the value of attribute bucket_last_reset.
-
#bucket_limit ⇒ Object
readonly
Returns the value of attribute bucket_limit.
-
#bucket_period ⇒ Object
readonly
Returns the value of attribute bucket_period.
-
#curr_count ⇒ Object
Returns the value of attribute curr_count.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#last_warning ⇒ Object
Returns the value of attribute last_warning.
-
#rate_last_reset ⇒ Object
Returns the value of attribute rate_last_reset.
-
#rate_limit ⇒ Object
readonly
Returns the value of attribute rate_limit.
-
#timeout_s ⇒ Object
readonly
Returns the value of attribute timeout_s.
Instance Method Summary collapse
-
#allow ⇒ Object
Checks if the bucket is free or full Returns:
true
if the bucket is freefalse
if the bucket is full. -
#expired ⇒ Object
Checks if bucket is expired Returns:
true
if the bucket is expiredfalse
if the bucket is not expired. -
#initialize(group, bucket_limit, bucket_period) ⇒ Bucket
constructor
A new instance of Bucket.
Constructor Details
#initialize(group, bucket_limit, bucket_period) ⇒ Bucket
Returns a new instance of Bucket.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 23 def initialize( group, bucket_limit, bucket_period) now = Time.now @group = group @bucket_count = 0 @bucket_count_total = 0 @bucket_last_reset = now @approx_rate_per_second = 0 @rate_last_reset = now @curr_count = 0 @last_warning = nil @bucket_limit = bucket_limit @bucket_period = bucket_period @rate_limit = bucket_limit/bucket_period @timeout_s = 2*bucket_period end |
Instance Attribute Details
#approx_rate_per_second ⇒ Object
Returns the value of attribute approx_rate_per_second.
21 22 23 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 21 def approx_rate_per_second @approx_rate_per_second end |
#bucket_count ⇒ Object
Returns the value of attribute bucket_count.
21 22 23 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 21 def bucket_count @bucket_count end |
#bucket_count_total ⇒ Object
Returns the value of attribute bucket_count_total.
21 22 23 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 21 def bucket_count_total @bucket_count_total end |
#bucket_last_reset ⇒ Object
Returns the value of attribute bucket_last_reset.
21 22 23 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 21 def bucket_last_reset @bucket_last_reset end |
#bucket_limit ⇒ Object (readonly)
Returns the value of attribute bucket_limit.
22 23 24 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 22 def bucket_limit @bucket_limit end |
#bucket_period ⇒ Object (readonly)
Returns the value of attribute bucket_period.
22 23 24 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 22 def bucket_period @bucket_period end |
#curr_count ⇒ Object
Returns the value of attribute curr_count.
21 22 23 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 21 def curr_count @curr_count end |
#group ⇒ Object (readonly)
Returns the value of attribute group.
22 23 24 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 22 def group @group end |
#last_warning ⇒ Object
Returns the value of attribute last_warning.
21 22 23 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 21 def last_warning @last_warning end |
#rate_last_reset ⇒ Object
Returns the value of attribute rate_last_reset.
21 22 23 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 21 def rate_last_reset @rate_last_reset end |
#rate_limit ⇒ Object (readonly)
Returns the value of attribute rate_limit.
22 23 24 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 22 def rate_limit @rate_limit end |
#timeout_s ⇒ Object (readonly)
Returns the value of attribute timeout_s.
22 23 24 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 22 def timeout_s @timeout_s end |
Instance Method Details
#allow ⇒ Object
Checks if the bucket is free or full Returns:
+true+ if the bucket is free
+false+ if the bucket is full
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 43 def allow if @bucket_limit == -1 return true end now = Time.now @curr_count += 1 @bucket_count_total += 1 time_lapsed = now - @rate_last_reset if time_lapsed.to_i >= 1 @approx_rate_per_second = @curr_count / time_lapsed @rate_last_reset = now @curr_count = 0 end if now.to_i / @bucket_period > @bucket_last_reset.to_i / @bucket_period reset_bucket end if @bucket_count == -1 or @bucket_count > @bucket_limit @bucket_count = -1 return false else @bucket_count += 1 true end end |
#expired ⇒ Object
Checks if bucket is expired Returns:
+true+ if the bucket is expired
+false+ if the bucket is not expired
75 76 77 78 |
# File 'lib/fluent/plugin/rate_limiter.rb', line 75 def expired now = Time.now now.to_i - @rate_last_reset.to_i > @timeout_s end |