Class: Labkit::RateLimit::Config::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/labkit/rate_limit/config/loader.rb

Overview

Loader runs the four steps of the config loader over a set of contract files and an optional rate limits file, and returns a Document.

parse and schema are Parser and Schema. The contract step lives here: it merges the contract files, checks that every rule uses only properties its limiter lists, that no override takes a protected name, and that a file rule carries no position. The rule step converts each proto Rule into a Rule with source: :file, which compiles patterns with RE2.

Strict mode, for CI, rejects on any failure. Lenient mode, for a running process, drops with a warning a rule for an unknown limiter, with an unknown property, with a protected name, or with a matcher whose only operator this binary does not know, and ignores a position. Any other failure rejects the whole file: the host then logs and runs on its defaults. Contract files are the application's own, so a contract mistake rejects in both modes.

Defined Under Namespace

Classes: Spec

Constant Summary collapse

ACTIONS =
{ ACTION_LIMIT: :limit, ACTION_LOG: :log, ACTION_SKIP: :skip }.freeze
OPERATORS =

The operators this version converts. A newer protos gem can decode an operator or an action outside these sets.

%i[eq ne re nre one_of none_of all].freeze
MAX_DEFAULT_RULES =

Default rules and properties per limiter, across every contract file. Schema caps one file's list; the merge must hold the same line.

Schema::FIELD_RULES.fetch("LimiterSpec.default_rules").fetch(:max_items)
MAX_PROPERTIES =
Schema::FIELD_RULES.fetch("LimiterSpec.available_properties").fetch(:max_items)

Instance Method Summary collapse

Constructor Details

#initialize(strict: false, logger: nil) ⇒ Loader

Returns a new instance of Loader.

Parameters:

  • strict (Boolean) (defaults to: false) —

    reject on any failure instead of dropping rules

  • logger (#warn, nil) (defaults to: nil) —

    receives each warning as it is recorded



38
39
40
41
42
43
44
# File 'lib/labkit/rate_limit/config/loader.rb', line 38

def initialize(strict: false, logger: nil)
  @strict = strict
  @logger = logger || RateLimit.config.logger || Labkit::Logging::JsonLogger.new($stdout)
  # load keeps the run's warnings on the instance, so two threads
  # sharing one Loader would mix them.
  @mutex = Mutex.new
end

Instance Method Details

#load(contracts:, rate_limits: nil) ⇒ Document

Parameters:

  • contracts (Array<String>) —

    paths of available_limiters files, in order

  • rate_limits (String, nil) (defaults to: nil) —

    path of the rate_limits file

Returns:

Raises:

  • (Error) —

    with the step that failed



50
51
52
# File 'lib/labkit/rate_limit/config/loader.rb', line 50

def load(contracts:, rate_limits: nil)
  @mutex.synchronize { load_locked(contracts, rate_limits) }
end