提高Ruby套接字性能

时间:2016-12-09 22:03:19

标签: ruby sockets tcp openssl

我正在使用TLS1.2进行套接字连接,并注意到从套接字读取数据时出现了一堆延迟。我怀疑它与Nagle算法有关,我试图在客户端和服务器上禁用它,但运气不好。

socket = TCPSocket.open(host,port)
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) # Nagle algorithm
ssl_context = OpenSSL::SSL::SSLContext.new
ssl_context.ssl_version = :SSLv23
ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
ssl_socket.sync_close = true
ssl_socket.connect

request = "some_data"

ssl_socket.puts(request)

while( c = ssl_socket.accept )
     c.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
     while( m = c.gets )
         c.puts( m.chomp )
     end
end


# The while loop above is currently failing, need help with it
# Here is the loop that is working but latency is 15-20 seconds

while line = ssl_socket.gets
     p line
end


ssl_socket.close

0 个答案:

没有答案