Sha256: d932fc83a27babb17892570630cbfce3fd28985ea5543eb41afc67db83ef73f6

Contents?: true

Size: 844 Bytes

Versions: 2

Compression:

Stored size: 844 Bytes

Contents

require 'resolv'
require 'mini_cache'

class ShortNameResolver
  def initialize(ttl:, logger:)
    @ttl = ttl
    @store = MiniCache::Store.new
    @logger = logger
  end

  private
  def resolve_cached(shortname)
    @store.get_or_set(shortname) do
      addresses = resolve(shortname)
      raise "Bad shortname '#{shortname}'" if addresses.empty?
      MiniCache::Data.new(addresses, expires_in: @ttl)
    end
  end

  private
  def resolve(shortname)
    addresses = Resolv::DNS.open do |dns|
      dns.getaddresses(shortname).map { |r| r.to_s }
    end

    @logger.info("Resolved shortname '#{shortname}' to addresses #{addresses}")

    return addresses
  end

  public
  def get_address(shortname)
    return resolve_cached(shortname).sample
  end

  public
  def get_addresses(shortname)
    return resolve_cached(shortname)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
logstash-output-clickhouse-0.1.3 lib/logstash/util/shortname_resolver.rb
logstash-output-clickhouse-0.1.1 lib/logstash/util/shortname_resolver.rb