Class: NbApiClient::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/nb_api_client/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nb_api_client/configuration.rb', line 43

def initialize
  @cache_store = (defined?(Rails) && Rails.respond_to?(:cache)) ? Rails.cache : nil
  @logger = (defined?(Rails) && Rails.respond_to?(:logger)) ? Rails.logger : nil
  @on_repeated_unauthorized = nil
  @unauthorized_error_threshold = 25
  @unauthorized_error_window = 5 * 60
  @short_circuit_in_test = true
  @max_rate_limit_retries = 3
  @max_token_retries = 2

  @rate_limit = ENV.fetch("API_RATE_LIMIT", 200).to_i
  @rate_limit_window_seconds = ENV.fetch("API_RATE_WINDOW", 10).to_i
  @rate_limit_max_wait_seconds = ENV.fetch("API_RATE_MAX_WAIT_SECONDS", 120).to_i
  @rate_limit_poll_interval = 0.25
  @rate_limit_max_jitter = 0.25
  @rate_limit_pool_size = ENV.fetch("RATE_LIMITER_POOL_SIZE", 6).to_i
  @redis_url = ENV.fetch("REDIS_URL", "redis://localhost:6379/0")
end

Instance Attribute Details

#cache_storeObject

Cache store used to count repeated "unauthorized" responses for a nation. Must respond to #increment(key, amount, options) and #exist?/#write (if you also use the cache in on_repeated_unauthorized). Defaults to Rails.cache.



8
9
10
# File 'lib/nb_api_client/configuration.rb', line 8

def cache_store
  @cache_store
end

#loggerObject

Logger for rate-limit and retry warnings. Defaults to Rails.logger.



11
12
13
# File 'lib/nb_api_client/configuration.rb', line 11

def logger
  @logger
end

#max_rate_limit_retriesObject

Returns the value of attribute max_rate_limit_retries.



30
31
32
# File 'lib/nb_api_client/configuration.rb', line 30

def max_rate_limit_retries
  @max_rate_limit_retries
end

#max_token_retriesObject

Returns the value of attribute max_token_retries.



31
32
33
# File 'lib/nb_api_client/configuration.rb', line 31

def max_token_retries
  @max_token_retries
end

#on_repeated_unauthorizedObject

Called with the nation whenever it has been deauthorized after too many consecutive "unauthorized" API responses. Use this to schedule re-authorization (e.g. a background job) or notify the account owner. Optional.



16
17
18
# File 'lib/nb_api_client/configuration.rb', line 16

def on_repeated_unauthorized
  @on_repeated_unauthorized
end

#rate_limitObject

Sliding-window rate limit: at most rate_limit requests per rate_limit_window_seconds, enforced across all processes via Redis.



35
36
37
# File 'lib/nb_api_client/configuration.rb', line 35

def rate_limit
  @rate_limit
end

#rate_limit_max_jitterObject

Returns the value of attribute rate_limit_max_jitter.



39
40
41
# File 'lib/nb_api_client/configuration.rb', line 39

def rate_limit_max_jitter
  @rate_limit_max_jitter
end

#rate_limit_max_wait_secondsObject

Returns the value of attribute rate_limit_max_wait_seconds.



37
38
39
# File 'lib/nb_api_client/configuration.rb', line 37

def rate_limit_max_wait_seconds
  @rate_limit_max_wait_seconds
end

#rate_limit_poll_intervalObject

Returns the value of attribute rate_limit_poll_interval.



38
39
40
# File 'lib/nb_api_client/configuration.rb', line 38

def rate_limit_poll_interval
  @rate_limit_poll_interval
end

#rate_limit_pool_sizeObject

Returns the value of attribute rate_limit_pool_size.



40
41
42
# File 'lib/nb_api_client/configuration.rb', line 40

def rate_limit_pool_size
  @rate_limit_pool_size
end

#rate_limit_window_secondsObject

Returns the value of attribute rate_limit_window_seconds.



36
37
38
# File 'lib/nb_api_client/configuration.rb', line 36

def rate_limit_window_seconds
  @rate_limit_window_seconds
end

#redis_urlObject

Returns the value of attribute redis_url.



41
42
43
# File 'lib/nb_api_client/configuration.rb', line 41

def redis_url
  @redis_url
end

#short_circuit_in_testObject

If true (the default), NbApiClient::Request#call returns {} immediately in Rails.env.test? instead of making a real HTTP request. This only applies when Request.call is invoked directly (not when it has been stubbed out in tests).



28
29
30
# File 'lib/nb_api_client/configuration.rb', line 28

def short_circuit_in_test
  @short_circuit_in_test
end

#unauthorized_error_thresholdObject

How many "unauthorized" responses within unauthorized_error_window before a nation is deauthorized.



20
21
22
# File 'lib/nb_api_client/configuration.rb', line 20

def unauthorized_error_threshold
  @unauthorized_error_threshold
end

#unauthorized_error_windowObject

Rolling window, in seconds, over which unauthorized_error_threshold is counted.



23
24
25
# File 'lib/nb_api_client/configuration.rb', line 23

def unauthorized_error_window
  @unauthorized_error_window
end