bridge_api 0.1.53 → 0.1.54

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a747c2ace7fb00d9374078f2ea7f15a8024adcadc3fa3b0b1b61b2d9cc431446
4
- data.tar.gz: 232e795f2ac605de1bcb2b22dbae65e1355e6f35f9895e3544e392d344f9b968
3
+ metadata.gz: 2d39d608d1b30d380fb6fdb79ad8284d5c2e2e1fd132623b89717cf94a9f9071
4
+ data.tar.gz: a8a72c682e3868563aa290f61b1dabb3295e34aff8ef6d9c61cff9f927a32bba
5
5
  SHA512:
6
- metadata.gz: d3bfd2c5c66bd5e3f20ddd2bc221ac4b69c3f14d53d5c4553d6a130795bb23971bf4c162d23b9a4ae7b43f8bee9ca8547973fa2cd4fe6dacc31ab0cd37172390
7
- data.tar.gz: e11c84963f136ce9613b5bdaa445810aa8aa0eff47e6c5c5037b8ab5536fe2cfc94d6e77b4df7fd8fdaca3c67869caf19bf79f6492186e7f757f1d3d86bc823b
6
+ metadata.gz: 8b93008180e1bdb8af16f4fbc88f4fa642cbe20e655fcac04dc34ea0c5ba20b1f38ef3b5c32e098224f48da463a7e7288fc7726c57232fc6e5c666d741c505b4
7
+ data.tar.gz: 10af8a2e42a4ca6db28abdeba4258bf6559a4b6f4ffc3907a0fe3d0f8820015a82b92a2001e08ae25cc3af2bd0653f4de58526f0301db6f0320db96d65a11d0a
@@ -4,7 +4,8 @@ require 'bridge_api/client'
4
4
  module BridgeAPI
5
5
  class << self
6
6
  require 'logging'
7
- attr_writer :enforce_rate_limits, :rate_limit_min, :rate_limits, :max_sleep_seconds, :min_sleep_seconds, :logger
7
+ attr_writer :enforce_rate_limits, :rate_limit_min, :rate_limits, :max_sleep_seconds, :min_sleep_seconds,
8
+ :logger, :master_rate_limit, :master_mutex, :rate_limit_threshold
8
9
 
9
10
  def configure
10
11
  yield self if block_given?
@@ -26,6 +27,18 @@ module BridgeAPI
26
27
  @max_sleep_seconds ||= 60
27
28
  end
28
29
 
30
+ def master_rate_limit
31
+ @master_rate_limit ||= false
32
+ end
33
+
34
+ def master_mutex
35
+ @master_mutex ||= Mutex.new
36
+ end
37
+
38
+ def rate_limit_threshold
39
+ @rate_limit_threshold ||= 10
40
+ end
41
+
29
42
  def logger
30
43
  return @logger if defined? @logger
31
44
  @logger = Logging.logger(STDOUT)
@@ -60,42 +60,49 @@ module BridgeAPI
60
60
  end
61
61
 
62
62
  def enforce_rate_limits
63
- return unless BridgeAPI.enforce_rate_limits && current_limit.present?
64
- return unless current_limit < BridgeAPI::rate_limit_min
65
-
66
- BridgeAPI.logger.debug("Current LIMIT: #{current_limit}")
67
- tts = ((BridgeAPI.rate_limit_min - current_limit) / 5).ceil
63
+ return unless BridgeAPI.enforce_rate_limits && limit_remaining.present?
64
+ return unless limit_remaining < BridgeAPI.rate_limit_threshold
65
+ tts = ((BridgeAPI.rate_limit_min - limit_remaining) / 5).ceil
68
66
  tts = BridgeAPI.min_sleep_seconds if tts < BridgeAPI.min_sleep_seconds
69
67
  tts = BridgeAPI.max_sleep_seconds if tts > BridgeAPI.max_sleep_seconds
70
68
  message = "Bridge API rate limit minimum #{BridgeAPI.rate_limit_min} reached for key: '#{config[:api_key]}'. "\
71
69
  "Sleeping for #{tts} second(s) to catch up ~zzZZ~. "\
72
- "Limit Remaining: #{current_limit}"
70
+ "Limit Remaining: #{limit_remaining}"
73
71
  BridgeAPI.logger.debug(message)
74
72
  sleep(tts)
75
73
  end
76
74
 
75
+ def using_master_rate_limit?
76
+ config[:master_rate_limit].present? || BridgeAPI.master_rate_limit.present?
77
+ end
78
+
77
79
  def apply_rate_limits(response)
78
80
  limit = response.headers['x-rate-limit-remaining']
79
81
  return if limit.nil?
80
- self.current_limit = limit.to_i
82
+ BridgeAPI.logger.debug("BRIDGE RATE LIMIT REMAINING: #{limit}")
83
+ self.limit_remaining = limit.to_i
81
84
  end
82
85
 
83
- def current_limit
84
- if config[:master_rate_limit]
85
- limit = PaulWalker::RateLimit.get(config[:api_key], config[:api_key])
86
- if limit.nil?
87
- PaulWalker::RateLimit.add(config[:api_key], config[:api_key], 0, BridgeAPI::rate_limit_min)
88
- limit = {current: 0}.with_indifferent_access
86
+ def limit_remaining
87
+ if using_master_rate_limit?
88
+ BridgeAPI.master_mutex.synchronize do
89
+ limit = PaulWalker::RateLimit.get(config[:api_key], config[:api_key])
90
+ if limit.nil?
91
+ PaulWalker::RateLimit.add(config[:api_key], config[:api_key], 0, BridgeAPI::rate_limit_min)
92
+ limit = {current: 0}.with_indifferent_access
93
+ end
94
+ limit['current']
89
95
  end
90
- limit['current']
91
96
  else
92
97
  BridgeAPI.rate_limits[config[:api_key]]
93
98
  end
94
99
  end
95
100
 
96
- def current_limit=(value)
97
- if config[:master_rate_limit]
98
- PaulWalker::RateLimit.add(config[:api_key], config[:api_key], value, BridgeAPI::rate_limit_min)
101
+ def limit_remaining=(value)
102
+ if using_master_rate_limit?
103
+ BridgeAPI.master_mutex.synchronize do
104
+ PaulWalker::RateLimit.add(config[:api_key], config[:api_key], value, BridgeAPI::rate_limit_min)
105
+ end
99
106
  else
100
107
  BridgeAPI.rate_limits[config[:api_key]] = value
101
108
  end
@@ -1,3 +1,3 @@
1
1
  module BridgeAPI
2
- VERSION = '0.1.53'.freeze unless defined?(BridgeAPI::VERSION)
2
+ VERSION = '0.1.54'.freeze unless defined?(BridgeAPI::VERSION)
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bridge_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.53
4
+ version: 0.1.54
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Shaffer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-22 00:00:00.000000000 Z
11
+ date: 2019-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler