Class: SlowDown::Configuration
- Inherits:
-
Object
- Object
- SlowDown::Configuration
- Defined in:
- lib/slow_down/configuration.rb
Constant Summary collapse
- CONCURRENCY_MULTIPLIER =
1
- DEFAULTS =
{ requests_per_second: 10, timeout: 5, raise_on_timeout: false, retries: 30, retry_strategy: :linear, redis: nil, redis_url: nil, redis_namespace: :slow_down, lock_namespace: :default, concurrency: nil, log_path: $stdout, log_level: Logger::UNKNOWN }
Instance Method Summary collapse
- #concurrency ⇒ Object
-
#initialize(options) ⇒ Configuration
constructor
A new instance of Configuration.
- #invalidate ⇒ Object
- #locks ⇒ Object
- #logger ⇒ Object
- #milliseconds_per_request ⇒ Object
- #milliseconds_per_request_per_lock ⇒ Object
- #redis ⇒ Object
- #seconds_per_retry(retry_count) ⇒ Object
- #seconds_per_retry_arr ⇒ Object
Constructor Details
#initialize(options) ⇒ Configuration
Returns a new instance of Configuration.
39 40 41 |
# File 'lib/slow_down/configuration.rb', line 39 def initialize() @options = DEFAULTS.merge() end |
Instance Method Details
#concurrency ⇒ Object
56 57 58 |
# File 'lib/slow_down/configuration.rb', line 56 def concurrency @concurrency ||= @options[:concurrency] || [1, requests_per_second.ceil * CONCURRENCY_MULTIPLIER].max end |
#invalidate ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/slow_down/configuration.rb', line 100 def invalidate @redis = nil @log_path = nil @log_level = nil @concurrency = nil @locks = nil @milliseconds_per_request = nil @milliseconds_per_request_per_lock = nil @seconds_per_retry = nil @seconds_per_retry_arr = nil end |
#locks ⇒ Object
60 61 62 63 64 |
# File 'lib/slow_down/configuration.rb', line 60 def locks @locks ||= concurrency.times.map do |i| [redis_namespace, "#{lock_namespace}_#{i}"].compact.join(":") end end |
#logger ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/slow_down/configuration.rb', line 43 def logger @logger ||= Logger.new(log_path).tap do |l| l.level = log_level l.formatter = proc do |severity, time, group_name, | "#{time},#{severity},##{Process.pid},#{group_name}: #{}\n" end end end |
#milliseconds_per_request ⇒ Object
66 67 68 |
# File 'lib/slow_down/configuration.rb', line 66 def milliseconds_per_request @milliseconds_per_request ||= 1000.0 / requests_per_second end |
#milliseconds_per_request_per_lock ⇒ Object
70 71 72 |
# File 'lib/slow_down/configuration.rb', line 70 def milliseconds_per_request_per_lock @milliseconds_per_request_per_lock ||= (milliseconds_per_request * concurrency).round end |
#redis ⇒ Object
52 53 54 |
# File 'lib/slow_down/configuration.rb', line 52 def redis @redis ||= @options[:redis] || Redis.new(url: redis_url || ENV.fetch("REDIS_URL")) end |
#seconds_per_retry(retry_count) ⇒ Object
74 75 76 |
# File 'lib/slow_down/configuration.rb', line 74 def seconds_per_retry(retry_count) seconds_per_retry_arr[retry_count - 1] end |
#seconds_per_retry_arr ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/slow_down/configuration.rb', line 78 def seconds_per_retry_arr @seconds_per_retry_arr ||= begin klass = case retry_strategy when :linear Strategy::Linear when :fibonacci Strategy::Fibonacci when :inverse_exponential Strategy::InverseExponential else retry_strategy end unless klass.is_a?(Class) && klass < Strategy::Base raise ConfigError, ":retry_strategy should be a class inheriting SlowDown::Strategy::Base" end klass.new(retries, timeout).normalized_series end end |