module Aws::RefreshingToken

Module/mixin used by token provider classes that can be refreshed. This provides basic refresh logic in a thread-safe manner. Classes mixing in this module are expected to implement a refresh method that populates the following instance variable:

@api private

Public Class Methods

new(options = {}) click to toggle source
# File lib/aws-sdk-core/refreshing_token.rb, line 18
def initialize(options = {})
  @mutex = Mutex.new
  @before_refresh = options.delete(:before_refresh) if Hash === options

  @before_refresh.call(self) if @before_refresh
  refresh
end

Public Instance Methods

expiration() click to toggle source

@return [Time,nil]

# File lib/aws-sdk-core/refreshing_token.rb, line 33
def expiration
  refresh_if_near_expiration
  @expiration
end
refresh!() click to toggle source

Refresh token. @return [void]

# File lib/aws-sdk-core/refreshing_token.rb, line 40
def refresh!
  @mutex.synchronize do
    @before_refresh.call(self) if @before_refresh
    refresh
  end
end
token() click to toggle source

@return [Token]

# File lib/aws-sdk-core/refreshing_token.rb, line 27
def token
  refresh_if_near_expiration
  @token
end

Private Instance Methods

near_expiration?() click to toggle source
# File lib/aws-sdk-core/refreshing_token.rb, line 62
def near_expiration?
  if @token && @token.expiration
    # are we within 5 minutes of expiration?
    (Time.now.to_i + 5 * 60) > @token.expiration.to_i
  else
    true
  end
end
refresh_if_near_expiration() click to toggle source

Refreshes token if it is within 5 minutes of expiration.

# File lib/aws-sdk-core/refreshing_token.rb, line 51
def refresh_if_near_expiration
  if near_expiration?
    @mutex.synchronize do
      if near_expiration?
        @before_refresh.call(self) if @before_refresh
        refresh
      end
    end
  end
end