Module: Legion::Extensions::Github::Helpers::TokenCache
- Includes:
- Cache::Helper
- Included in:
- Client
- Defined in:
- lib/legion/extensions/github/helpers/token_cache.rb
Constant Summary collapse
- TOKEN_BUFFER_SECONDS =
300
Instance Method Summary collapse
- #fetch_token(auth_type:, installation_id: nil) ⇒ Object
- #mark_rate_limited(auth_type:, reset_at:) ⇒ Object
- #rate_limited?(auth_type:) ⇒ Boolean
- #store_token(token:, auth_type:, expires_at:, installation_id: nil, metadata: {}) ⇒ Object
Instance Method Details
#fetch_token(auth_type:, installation_id: nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/legion/extensions/github/helpers/token_cache.rb', line 25 def fetch_token(auth_type:, installation_id: nil, **) key = token_cache_key(auth_type, installation_id) entry = token_cache_read(key) entry = token_cache_read(token_cache_key(auth_type, nil)) if entry.nil? && installation_id return nil unless entry expires = begin Time.parse(entry[:expires_at].to_s) rescue StandardError => _e nil end return nil if expires && expires < Time.now + TOKEN_BUFFER_SECONDS entry end |
#mark_rate_limited(auth_type:, reset_at:) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/legion/extensions/github/helpers/token_cache.rb', line 43 def mark_rate_limited(auth_type:, reset_at:, **) entry = { reset_at: reset_at.respond_to?(:iso8601) ? reset_at.iso8601 : reset_at } ttl = [(reset_at.respond_to?(:to_i) ? reset_at.to_i - Time.now.to_i : 300), 10].max key = "github:rate_limit:#{auth_type}" cache_set(key, entry, ttl: ttl) if cache_connected? local_cache_set(key, entry, ttl: ttl) if local_cache_connected? end |
#rate_limited?(auth_type:) ⇒ Boolean
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/legion/extensions/github/helpers/token_cache.rb', line 51 def rate_limited?(auth_type:, **) key = "github:rate_limit:#{auth_type}" entry = if cache_connected? cache_get(key) elsif local_cache_connected? local_cache_get(key) end return false unless entry reset = begin Time.parse(entry[:reset_at].to_s) rescue StandardError => _e nil end reset.nil? || reset > Time.now end |
#store_token(token:, auth_type:, expires_at:, installation_id: nil, metadata: {}) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/legion/extensions/github/helpers/token_cache.rb', line 15 def store_token(token:, auth_type:, expires_at:, installation_id: nil, metadata: {}, **) entry = { token: token, auth_type: auth_type, expires_at: expires_at.respond_to?(:iso8601) ? expires_at.iso8601 : expires_at, installation_id: installation_id, metadata: } ttl = [(expires_at.respond_to?(:to_i) ? expires_at.to_i - Time.now.to_i : 3600), 60].max key = token_cache_key(auth_type, installation_id) cache_set(key, entry, ttl: ttl) if cache_connected? local_cache_set(key, entry, ttl: ttl) if local_cache_connected? end |