2014-06-29 27 views
1

我用ruby gserver創建了一個TCPServer。Ruby TCPServer總是延遲dns反向查找? - 如何禁用?

每當我連接到服務器時,連接建立需要2-4秒。

這隻會發生,如果我從遠程機器連接。

來自同一臺機器的連接已經運行該服務將發送immidiate響應。

對於同一臺機器上的連接,如果我通過localhost或通過機器ip連接,則沒有區別。


我認爲延遲取決於反向查找,但不能本地化原因。

在gserver.rb它是線263

client = @tcpServer.accept 

這裏發生的延遲,我不知道什麼是這種方法。


我將所有在測試過程中使用的機器添加到本地主機文件。但這沒有改變。

同樣的情況,使用的WEBrick的時候,我想也設置

BasicSocket.do_not_reverse_lookup = true 

,以及直接對得到的服務器套接字

Socket.do_not_reverse_lookup = true 

,以及對客戶端連接插座

client.do_not_reverse_lookup = true 

但這並沒有改變任何延誤。


只要建立連接,remote_host和remote_ip的值就會被解析並按照hosts文件中的定義進行解析。


我試過在ubuntu 14.04上運行ruby 2.2.1以及運行debian wheezy的ruby 1.9.3。

相同的行爲 - (很長)延遲連接服務。

問:如何解決在TCPServer上的查找/禁用問題?

+0

我做了與webrick相同的測試 - 同樣的行爲,當從遠程連接它有2-4秒的延遲。 –

+0

我還配置了我的系統nsswitch.conf一次只使用文件,但再次沒有影響 - 仍然延遲 –

回答

0

問題取決於我在MAC OSX Mav上運行的客戶端機器。

已使用的telnet客戶端嘗試打開IPv6連接,然後嘗試IPv4。

爲了解決延遲,只是

telnet -4 my-server 3333 

打開連接我建立一個小型的連接回聲servive這裏你可以查看解析和時序。

如果更改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" 

感謝來自#ruby-lang irc的drbrain指出我的IPv6問題。