lib/easy_throttle.rb in EasyThrottle-0.0.2 vs lib/easy_throttle.rb in EasyThrottle-0.1.0
- old
+ new
@@ -1,26 +1,21 @@
# frozen_string_literal: true
-require 'yaml'
require 'redis'
class EasyThrottle
DEFAULT_OPTIONS = { interval: 60, limit: 1, burst: 20 }.freeze
- LIMITS = {
- # 'endpointName' => { interval: 10, limit: 1, burst: 1 },
- }.freeze
def self.with_throttling(endpoint, prefix, options = DEFAULT_OPTIONS, &block)
new(endpoint, prefix, options).with_throttling { block.call }
end
def initialize(endpoint, prefix, options = DEFAULT_OPTIONS)
- @config = YAML.load_file('lib/config.yml')
@endpoint = endpoint
@prefix = prefix
@redis = Redis.new
- @options = LIMITS.fetch(endpoint, options)
+ @options = EasyThrottle.configuration.limits.fetch(endpoint, options)
end
def with_throttling(&block)
retry_count ||= 0
result = nil
@@ -32,11 +27,11 @@
else
sleep 0.1
end
end
result
- rescue StandardError => e
+ rescue EasyThrottle.configuration.errors => e
if e.code == 429
extend_key_duration
retry
end
@@ -44,11 +39,11 @@
raise if (retry_count += 1) > 5
extend_key_duration
retry
end
- raise e
+ raise EasyThrottle::Error.new(msg: e.message, error: e)
end
def pool
max_ttl = (options[:burst] - 1) * options[:interval] * 1000
burst_ttl = redis.pttl(burst_key)
@@ -67,13 +62,7 @@
"#{prefix}:#{endpoint}:burst"
end
def extend_key_duration
redis.psetex(burst_key, (options[:interval] + 1) * options[:burst] * 1000, true)
- end
-
- def constantize(str)
- str.split('::').inject(Object) do |mod, class_name|
- mod.const_get(class_name)
- end
end
end