Module: ExcessFlow

Defined in:
lib/excess_flow.rb,
lib/excess_flow/strategy.rb,
lib/excess_flow/constants.rb,
lib/excess_flow/global_mutex.rb,
lib/excess_flow/configuration.rb,
lib/excess_flow/failed_execution.rb,
lib/excess_flow/redis_connection.rb,
lib/excess_flow/throttled_executor.rb,
lib/excess_flow/configuration_error.rb,
lib/excess_flow/fixed_window_strategy.rb,
lib/excess_flow/throttle_configuration.rb,
lib/excess_flow/sliding_window_strategy.rb,
lib/excess_flow/rate_limited_execution_result.rb

Overview

Copyright 2019 ConvertKit, LLC

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: RedisConnection Classes: Configuration, ConfigurationError, FailedExecution, FixedWindowStrategy, GlobalMutex, RateLimitedExecutionResult, SlidingWindowStrategy, Strategy, ThrottleConfiguration, ThrottledExecutor

Constant Summary collapse

CONFIGURATION_ERROR_MESSAGE =
'Invalid arguments provided. Please refer to README.md'
COUNTER_PREFIX =
'excess_flow::counter::'
DEFAULT_CONNECTION_POOL =
100
DEFAULT_CONNECTION_TIMEOUT =
3
DEFAULT_REDIS_URL =
'redis://localhost:6379/1'
LOCK_PREFIX =
'excess_flow::lock::'
MUTEX_LOCK_TIME =
1
MUTEX_SLEEP_TIME =
1 / 100_000
VERSION =
'1.0.2'

Class Method Summary collapse

Class Method Details

.configurationConfiguration

Provides access to cache’s configuration.

Returns:



58
59
60
# File 'lib/excess_flow.rb', line 58

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ void

This method returns an undefined value.

API to configure cache dynamically during runtime.

Examples:

Configure during runtime changing redis URL

ExcessFlow.configure { |c| c.redis_url = 'foobar' }

Yields:

  • (configuration)

    Takes in a block of code of code that is setting or changing configuration values



71
72
73
74
# File 'lib/excess_flow.rb', line 71

def configure
  yield(configuration)
  nil
end

.redis {|redis| ... } ⇒ Object

API to communicate with Redis database backing cache up.

Examples:

Store a value in Redis at given key

Store.redis { |r| r.set('meaning_of_life', 42) }

Yields:

Returns:

  • Returns a result of interaction with Redis



84
85
86
87
88
# File 'lib/excess_flow.rb', line 84

def redis
  redis_connection_pool.with do |connection|
    yield connection
  end
end

.redis_connection_poolConnectionPool

Accessor to connection pool. Defined on top level so it can be memoized on the topmost level

Returns:

  • (ConnectionPool)

    ConnectionPool object from connection_pool gem



94
95
96
# File 'lib/excess_flow.rb', line 94

def redis_connection_pool
  @redis_connection_pool ||= ExcessFlow::RedisConnection.connection_pool
end

.throttle(args, &block) ⇒ ExcessFlow::RateLimitedExecutionResult

Executes passed in block of code rate limited using specified strategy and arguments. Different call types can be differentiated and configured using arguments.

Parameters:

  • key (String)

    key to identify your request to limit. Requests that are identified by same key share limits

  • limit (Integer)

    number of requests that can be made in the specified time window

  • ttl (Integer)

    size of window in seconds; how long it will take until the limits are reset

  • strategy (Symbol)

    (optional) specifies which strategy to use to rate limit requests. Optional. Defaults to :fixed_window. Supported strategies are :fixed_window and :sliding_window.

Returns:

  • (ExcessFlow::RateLimitedExecutionResult)

    Execution result object that will hold result of your execution if it was under the limit. Accessible by calling ‘result` method. This object has two methods as part of it’s public API: ‘result` (returns either result of execution or ExcessFlow::FailedExecution if limits were breached) and `success?` (returns `true` if request was within limits and else returns `false`).



118
119
120
# File 'lib/excess_flow.rb', line 118

def throttle(args, &block)
  ExcessFlow::ThrottledExecutor.select_strategy_and_execute(args, &block)
end