Class: Pecorino::Adapters::BaseAdapter
- Inherits:
-
Object
- Object
- Pecorino::Adapters::BaseAdapter
- Defined in:
- lib/pecorino/adapters/base_adapter.rb
Overview
An adapter allows Pecorino throttles, leaky buckets and other resources to interfact to a data storage backend - a database, usually.
Direct Known Subclasses
Instance Method Summary collapse
-
#add_tokens(key:, capacity:, leak_rate:, n_tokens:) ⇒ Array
Adds tokens to the leaky bucket.
-
#add_tokens_conditionally(key:, capacity:, leak_rate:, n_tokens:) ⇒ Array
Adds tokens to the leaky bucket conditionally.
-
#blocked_until(key:) ⇒ Object
Returns the time until which a block for a given key is in effect.
-
#create_tables(active_record_schema) ⇒ Object
Creates the database tables for Pecorino to operate, or initializes other schema-like resources the adapter needs to operate.
-
#prune ⇒ void
Deletes leaky buckets which have an expiry value prior to now and throttle blocks which have now lapsed.
-
#set_block(key:, block_for:) ⇒ Object
Sets a timed block for the given key - this is used when a throttle fires.
-
#state(key:, capacity:, leak_rate:) ⇒ Array
Returns the state of a leaky bucket.
Instance Method Details
#add_tokens(key:, capacity:, leak_rate:, n_tokens:) ⇒ Array
Adds tokens to the leaky bucket. The return value is a tuple of two values: the current level (Float) and whether the bucket is now at capacity (Boolean)
25 26 27 |
# File 'lib/pecorino/adapters/base_adapter.rb', line 25 def add_tokens(key:, capacity:, leak_rate:, n_tokens:) [0, false] end |
#add_tokens_conditionally(key:, capacity:, leak_rate:, n_tokens:) ⇒ Array
Adds tokens to the leaky bucket conditionally. If there is capacity, the tokens will be added. If there isn’t - the fillup will be rejected. The return value is a triplet of the current level (Float), whether the bucket is now at capacity (Boolean) and whether the fillup was accepted (Boolean)
39 40 41 |
# File 'lib/pecorino/adapters/base_adapter.rb', line 39 def add_tokens_conditionally(key:, capacity:, leak_rate:, n_tokens:) [0, false, false] end |
#blocked_until(key:) ⇒ Object
Returns the time until which a block for a given key is in effect. If there is no block in effect, the method should return ‘nil`. The return value is either a `Time` or `nil`
53 54 |
# File 'lib/pecorino/adapters/base_adapter.rb', line 53 def blocked_until(key:) end |
#create_tables(active_record_schema) ⇒ Object
Creates the database tables for Pecorino to operate, or initializes other schema-like resources the adapter needs to operate
64 65 |
# File 'lib/pecorino/adapters/base_adapter.rb', line 64 def create_tables(active_record_schema) end |
#prune ⇒ void
This method returns an undefined value.
Deletes leaky buckets which have an expiry value prior to now and throttle blocks which have now lapsed
59 60 |
# File 'lib/pecorino/adapters/base_adapter.rb', line 59 def prune end |
#set_block(key:, block_for:) ⇒ Object
Sets a timed block for the given key - this is used when a throttle fires. The return value is not defined - the call should always succeed.
47 48 |
# File 'lib/pecorino/adapters/base_adapter.rb', line 47 def set_block(key:, block_for:) end |
#state(key:, capacity:, leak_rate:) ⇒ Array
Returns the state of a leaky bucket. The state should be a tuple of two values: the current level (Float) and whether the bucket is now at capacity (Boolean)
13 14 15 |
# File 'lib/pecorino/adapters/base_adapter.rb', line 13 def state(key:, capacity:, leak_rate:) [0, false] end |