Class: ActiveRecordApi::Request::TokenCache
- Inherits:
-
Object
- Object
- ActiveRecordApi::Request::TokenCache
- Includes:
- ActiveAttr::Model
- Defined in:
- lib/active_record_api/request/token_cache.rb
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
Instance Method Summary collapse
- #acquire_lock ⇒ Object
- #debug_log(message) ⇒ Object
- #fetch_token(token_proc) ⇒ Object
- #flush ⇒ Object
- #release_lock ⇒ Object
- #retrieve_token_from_redis(token_proc) ⇒ Object
- #unique_key ⇒ Object
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
6 7 8 |
# File 'lib/active_record_api/request/token_cache.rb', line 6 def debug @debug end |
Instance Method Details
#acquire_lock ⇒ Object
65 66 67 68 69 70 |
# File 'lib/active_record_api/request/token_cache.rb', line 65 def acquire_lock @redis_client.set 'auth_token_lock', unique_key, nx: true, ex: 60 current_lock_value = @redis_client.get 'auth_token_lock' Rails.logger.info "#{current_lock_value} == #{unique_key}" if debug current_lock_value == unique_key end |
#debug_log(message) ⇒ Object
61 62 63 |
# File 'lib/active_record_api/request/token_cache.rb', line 61 def debug_log() Rails.logger.info if debug end |
#fetch_token(token_proc) ⇒ Object
19 20 21 |
# File 'lib/active_record_api/request/token_cache.rb', line 19 def fetch_token(token_proc) retrieve_token_from_redis(token_proc) end |
#flush ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_record_api/request/token_cache.rb', line 8 def flush return unless defined? AUTH_REDIS_POOL debug_log 'flushing token cache' Honeybadger.notify(StandardError.new('token cache has been flushed')) AUTH_REDIS_POOL.with do |client| @redis_client = client @redis_client.del('auth_token') release_lock end end |
#release_lock ⇒ Object
72 73 74 |
# File 'lib/active_record_api/request/token_cache.rb', line 72 def release_lock @redis_client.del 'auth_token_lock' end |
#retrieve_token_from_redis(token_proc) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/active_record_api/request/token_cache.rb', line 23 def retrieve_token_from_redis(token_proc) debug_log 'looking?' unless defined? AUTH_REDIS_POOL debug_log 'no auth redis pool' Honeybadger.notify(StandardError.new('no auth redis pool for token cache')) return token_proc.call() end debug_log 'looking for token in cache' AUTH_REDIS_POOL.with do |client| @redis_client = client @token = nil @retry = 0 while @token.nil? && @retry < 5 @retry += 1 @token = @redis_client.get('auth_token') debug_log "retry ##{@retry}" debug_log "found token: #{@token}" if @token.nil? if acquire_lock begin debug_log "acquired lock" Honeybadger.notify(StandardError.new('acquired lock and fetching token for cache')) @token = token_proc.call() @redis_client.set 'auth_token', @token ensure debug_log 'releasing lock' release_lock end else debug_log 'waiting for token to be cached' sleep 3 end end end end @token end |
#unique_key ⇒ Object
76 77 78 |
# File 'lib/active_record_api/request/token_cache.rb', line 76 def unique_key @unique_key ||= rand.to_s end |