Sha256: aaf2cf2902f6d8018164d24ba4c796f47ef2a7e09e3a66d6f9dc428698551908

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

class SentrySmartSampler
  class ThrottlingPerErrorRegistry
    attr_reader :default_throttling_errors_number_threshold, :default_throttling_time_unit, :registry
    private     :default_throttling_errors_number_threshold, :default_throttling_time_unit, :registry

    def initialize(default_throttling_errors_number_threshold, default_throttling_time_unit)
      @default_throttling_errors_number_threshold = default_throttling_errors_number_threshold
      @default_throttling_time_unit = default_throttling_time_unit
      @registry = []
    end

    def declare(throttable, time_unit:, threshold:)
      registry << Registration.new(throttable: throttable, time_unit: time_unit, threshold: threshold)
    end

    def throttling_registration_for(error)
      registry.find(-> { default_registration }) { |registration| registration.matches?(error) }
    end

    private

    def default_registration
      Registration.new(throttable: nil, time_unit: default_throttling_time_unit,
        threshold: default_throttling_errors_number_threshold)
    end

    class Registration
      attr_reader :throttable, :threshold, :time_unit

      def initialize(throttable:, threshold:, time_unit:)
        @throttable = throttable
        @threshold = threshold
        @time_unit = time_unit
      end

      def matches?(matchable_error)
        if throttable.is_a?(Regexp) || throttable.respond_to?(:to_str)
          matchable_error.message.scan(throttable).any?
        else
          matchable_error.is_a?(throttable)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sentry-smart-sampler-0.1.0 lib/sentry_smart_sampler/throttling_per_error_registry.rb