简体   繁体   English

Ruby TCPServer总是延迟dns反向查找吗? -如何禁用?

[英]Ruby TCPServer always delay on dns reverse lookup? - how to disable?

I created a TCPServer with ruby gserver. 我用ruby gserver创建了一个TCPServer。

Everytime I connect remotly to the server, it takes 2-4 seconds until connection is established. 每次我远程连接到服务器时,连接建立都需要2-4秒。

This is only happen if I connect from remote machine. 仅当我从远程计算机连接时,才会发生这种情况。

Connection from same machine has running the service will send immidiate response. 来自运行该服务的同一台计算机的连接将发送紧急响应。

For the connection on same machine there is no difference if I connect via localhost or via the machines ip. 对于同一台机器上的连接,如果我通过本地主机或通过机器ip连接,则没有任何区别。


I think delay depends on reverse lookup but can not localize why. 我认为延迟取决于反向查找,但无法定位原因。

in gserver.rb it is line 263 在gserver.rb中是第263行

client = @tcpServer.accept

Here the delay occurs, I do not know what is in this method. 在这里发生延迟,我不知道这种方法是什么。


I added all machines which are used during tests to the local hosts file. 我将测试期间使用的所有计算机添加到本地主机文件中。 But that changed nothing. 但这并没有改变。

Same happens when using Webrick, I tried to set also 使用Webrick时也会发生同样的情况,我也尝试设置

BasicSocket.do_not_reverse_lookup = true

as well as direct on the resulting server socket 以及直接在生成的服务器套接字上

Socket.do_not_reverse_lookup = true

as well as on client connection socket 以及客户端连接套接字上

client.do_not_reverse_lookup = true

But that also changed nothing on delay. 但这也没有改变任何延迟。


Whenever connection is established, the values of remote_host and remote_ip are resolved and as defined in hosts file. 只要建立连接,就会解析remote_host和remote_ip的值,并在hosts文件中定义。


I tried that running ruby 2.2.1 on ubuntu 14.04 as well as ruby 1.9.3 running debian wheezy. 我尝试在Ubuntu 14.04上运行ruby 2.2.1以及运行debian wheezy的ruby 1.9.3。

Same behavior - (long) delay on connecting service. 行为相同-连接服务的延迟时间较长。

Q: How to fix that / disable lookup on TCPServer? 问:如何解决/禁用TCPServer上的查找?

The problem depends on my client machine where I run on MAC OSX Mav. 问题取决于我在MAC OSX Mav上运行的客户端计算机。

The used telnet client tries to open IPv6 connection and afterwards IPv4. 使用的telnet客户端尝试打开IPv6连接,然后再尝试打开IPv4。

To solve the delay, just open connection with 要解决延迟,只需打开与

telnet -4 my-server 3333

I have build a small connect echo servive where you can check resolves and timings. 我建立了一个小型的连接回显服务,您可以在其中检查解析和计时。

If you change NO_REVERSE_LOOKUP you will get IPs or ADDRESSes and if not resolveable, different response times. 如果更改NO_REVERSE_LOOKUP,则将获得IP或地址,并且如果无法解析,将获得不同的响应时间。

require 'socket'

NO_REVERSE_LOOKUP = true
CONNECT_PORT = 3333

puts "#{Time.now} Starting service on port: #{CONNECT_PORT}"

# the full hell - just to test if anything meets what we want
TCPServer.do_not_reverse_lookup = NO_REVERSE_LOOKUP
BasicSocket.do_not_reverse_lookup = NO_REVERSE_LOOKUP
Socket.do_not_reverse_lookup = NO_REVERSE_LOOKUP

srv = TCPServer.open(CONNECT_PORT)

puts "#{Time.now} Waiting for client"

client = srv.accept

puts "#{Time.now} Client connected"

client.do_not_reverse_lookup = NO_REVERSE_LOOKUP

client.print "Hello connected\n"

# in case that we disabled reverse lookup, we should only receive IP Adresses

puts "#{Time.now} Getting server address infos" 

puts "SERVER INFO:"
puts NO_REVERSE_LOOKUP ? client.addr(:numeric) : client.addr(:hostname)

puts ""

puts "#{Time.now} Getting remote client infos"

puts "REMOTE INFO:"
puts NO_REVERSE_LOOKUP ? client.peeraddr(:numeric) : client.peeraddr(:hostname)

###

puts "#{Time.now} Closing connection"

client.close

puts "#{Time.now} End"

Thanks to drbrain from #ruby-lang irc for pointing me to the IPv6 problem. 感谢#ruby-lang irc的drbrain向我指出了IPv6问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM