github.com/anycable/anycable-go@v1.5.1/etc/dns/server.rb (about) 1 require "bundler/inline" 2 3 retried = false 4 begin 5 gemfile(retried, quiet: ENV["LOG"] != "1") do 6 source "https://rubygems.org" 7 8 gem "async-dns" 9 end 10 rescue Gem::MissingSpecError, Bundler::GemNotFound 11 raise if retried 12 13 retried = true 14 retry 15 end 16 17 require 'async/dns' 18 19 IN = Resolv::DNS::Resource::IN 20 21 RPC_ADDRESSES = %w[ 22 127.0.0.1 23 127.0.0.2 24 127.0.0.3 25 127.0.0.4 26 ] 27 28 class Server < Async::DNS::Server 29 def process(name, resource_class, transaction) 30 if resource_class == IN::A && name.match?(%r{anycable-rpc.local}) 31 addresses = alive_rpcs 32 if addresses.empty? 33 return transaction.fail!(:NXDomain) 34 end 35 36 transaction.append_question! 37 addresses.each do 38 transaction.add([IN::A.new(_1)], {}) 39 end 40 41 @logger.info "Resolved RPCs: #{addresses}" 42 43 return 44 end 45 46 transaction.fail!(:NXDomain) 47 end 48 49 private 50 51 def alive_rpcs 52 RPC_ADDRESSES.select do 53 Socket.tcp(_1, 50051, connect_timeout: 0.1).close 54 true 55 rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ETIMEDOUT, SocketError 56 false 57 end 58 end 59 end 60 61 server = Server.new([[:udp, '127.0.0.1', 2346], [:tcp, '127.0.0.1', 2346]]) 62 server.run