class Aws::EndpointCache

@api private a LRU cache caching endpoints data

Constants

MAX_ENTRIES

default cache entries limit

MAX_THREADS

default max threads pool size

Attributes

max_entries[R]

@return [Integer] Max size limit of cache

max_threads[R]

@return [Integer] Max count of polling threads

pool[R]

return [Hash] Polling threads pool

Public Class Methods

new(options = {}) click to toggle source
# File lib/aws-sdk-core/endpoint_cache.rb, line 14
def initialize(options = {})
  @max_entries = options[:max_entries] || MAX_ENTRIES
  @entries = {} # store endpoints
  @max_threads = options[:max_threads] || MAX_THREADS
  @pool = {} # store polling threads
  @mutex = Mutex.new
  @require_identifier = nil # whether endpoint operation support identifier
end

Public Instance Methods

[](key) click to toggle source

@param [String] key @return [Endpoint]

# File lib/aws-sdk-core/endpoint_cache.rb, line 34
def [](key)
  @mutex.synchronize do
    # fetching an existing endpoint delete it and then append it
    endpoint = @entries[key]
    if endpoint
      @entries.delete(key)
      @entries[key] = endpoint
    end
    endpoint
  end
end
[]=(key, value) click to toggle source

@param [String] key @param [Hash] value

# File lib/aws-sdk-core/endpoint_cache.rb, line 48
def []=(key, value)
  @mutex.synchronize do
    # delete the least recent used endpoint when cache is full
    unless @entries.size < @max_entries
      old_key, = @entries.shift
      delete_polling_thread(old_key)
    end
    # delete old value if exists
    @entries.delete(key)
    @entries[key] = Endpoint.new(value.to_hash)
  end
end
delete(key) click to toggle source

remove entry only @param [String] key

# File lib/aws-sdk-core/endpoint_cache.rb, line 82
def delete(key)
  @mutex.synchronize do
    @entries.delete(key)
  end
end
delete_polling_thread(key) click to toggle source

kill the old polling thread and remove it from pool @param [String] key

# File lib/aws-sdk-core/endpoint_cache.rb, line 90
def delete_polling_thread(key)
  Thread.kill(@pool[key]) if threads_key?(key)
  @pool.delete(key)
end
extract_key(ctx) click to toggle source

extract the key to be used in the cache from request context @param [RequestContext] ctx @return [String]

# File lib/aws-sdk-core/endpoint_cache.rb, line 109
def extract_key(ctx)
  parts = []
  # fetching from cred provider directly gives warnings
  parts << ctx.config.credentials.credentials.access_key_id
  if _endpoint_operation_identifier(ctx)
    parts << ctx.operation_name
    ctx.operation.input.shape.members.inject(parts) do |p, (name, ref)|
      p << ctx.params[name] if ref['endpointdiscoveryid']
      p
    end
  end
  parts.join('_')
end
key?(key) click to toggle source

checking whether an unexpired endpoint key exists in cache @param [String] key @return [Boolean]

# File lib/aws-sdk-core/endpoint_cache.rb, line 64
def key?(key)
  @mutex.synchronize do
    if @entries.key?(key) && (@entries[key].nil? || @entries[key].expired?)
      @entries.delete(key)
    end
    @entries.key?(key)
  end
end
stop_polling!() click to toggle source

kill all polling threads

# File lib/aws-sdk-core/endpoint_cache.rb, line 135
def stop_polling!
  @pool.each { |_, t| Thread.kill(t) }
  @pool = {}
end
threads_key?(key) click to toggle source

checking whether an polling thread exist for the key @param [String] key @return [Boolean]

# File lib/aws-sdk-core/endpoint_cache.rb, line 76
def threads_key?(key)
  @pool.key?(key)
end
update(key, ctx) click to toggle source

update cache with requests (using service endpoint operation) to fetch endpoint list (with identifiers when available) @param [String] key @param [RequestContext] ctx

# File lib/aws-sdk-core/endpoint_cache.rb, line 99
def update(key, ctx)
  resp = _request_endpoint(ctx)
  if resp && resp.endpoints
    resp.endpoints.each { |e| self[key] = e }
  end
end
update_polling_pool(key, thread) click to toggle source

update polling threads pool param [String] key param [Thread] thread

# File lib/aws-sdk-core/endpoint_cache.rb, line 126
def update_polling_pool(key, thread)
  unless @pool.size < @max_threads
    _, thread = @pool.shift
    Thread.kill(thread)
  end
  @pool[key] = thread
end

Private Instance Methods

_endpoint_operation_identifier(ctx) click to toggle source
# File lib/aws-sdk-core/endpoint_cache.rb, line 164
def _endpoint_operation_identifier(ctx)
  return @require_identifier unless @require_identifier.nil?

  operation_name = ctx.config.api.endpoint_operation
  operation = ctx.config.api.operation(operation_name)
  @require_identifier = operation.input.shape.members.any?
end
_request_endpoint(ctx) click to toggle source
# File lib/aws-sdk-core/endpoint_cache.rb, line 142
def _request_endpoint(ctx)
  params = {}
  if _endpoint_operation_identifier(ctx)
    # build identifier params when available
    params[:operation] = ctx.operation.name
    ctx.operation.input.shape.members.inject(params) do |p, (name, ref)|
      if ref['endpointdiscoveryid']
        p[:identifiers] ||= {}
        p[:identifiers][ref.location_name] = ctx.params[name]
      end
      p
    end
  end

  begin
    endpoint_operation_name = ctx.config.api.endpoint_operation
    ctx.client.send(endpoint_operation_name, params)
  rescue Aws::Errors::ServiceError
    nil
  end
end