module Aws::RefreshingCredentials

def refresh_if_near_expiration!

but attempt a refresh in the background.
Otherwise, if we're approaching expiration, use the existing credentials
If we are near to expiration, block while getting new credentials.
Refreshes credentials asynchronously and synchronously.
def refresh_if_near_expiration!
  # Note: This check is an optimization. Rather than acquire the mutex on every #refresh_if_near_expiration
  # call, we check before doing so, and then we check within the mutex to avoid a race condition.
  # See issue: https://github.com/aws/aws-sdk-ruby/issues/2641 for more info.
  if near_expiration?(sync_expiration_length)
    @mutex.synchronize do
      if near_expiration?(sync_expiration_length)
        @before_refresh.call(self) if @before_refresh
        refresh
      end
    end
  elsif @async_refresh && near_expiration?(async_expiration_length)
    unless @mutex.locked?
      Thread.new do
        @mutex.synchronize do
          if near_expiration?(async_expiration_length)
            @before_refresh.call(self) if @before_refresh
            refresh
          end
        end
      end
    end
  end
end