Class: Pecorino::Adapters::BaseAdapter

Inherits:
Object
  • Object
show all
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

RedisAdapter

Instance Method Summary collapse

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)

Parameters:

  • key (String)

    the key of the leaky bucket

  • capacity (Float)

    the capacity of the leaky bucket to limit to

  • leak_rate (Float)

    how many tokens leak out of the bucket per second

  • n_tokens (Float)

    how many tokens to add

Returns:

  • (Array)


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)

Parameters:

  • key (String)

    the key of the leaky bucket

  • capacity (Float)

    the capacity of the leaky bucket to limit to

  • leak_rate (Float)

    how many tokens leak out of the bucket per second

  • n_tokens (Float)

    how many tokens to add

Returns:

  • (Array)


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`

Parameters:

  • key (String)

    the key of the block



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

#prunevoid

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.

Parameters:

  • key (String)

    the key of the block

  • block_for (#to_f, Active Support Duration)

    the duration of the block, in seconds



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)

Parameters:

  • key (String)

    the key of the leaky bucket

  • capacity (Float)

    the capacity of the leaky bucket to limit to

  • leak_rate (Float)

    how many tokens leak out of the bucket per second

Returns:

  • (Array)


13
14
15
# File 'lib/pecorino/adapters/base_adapter.rb', line 13

def state(key:, capacity:, leak_rate:)
  [0, false]
end