gpt4 book ai didi

当超过 250 个并发连接时,Ruby SSL TCP 服务器卡住

转载 作者:可可西里 更新时间:2023-11-01 02:54:17 25 4
gpt4 key购买 nike

我正在用 ruby​​ 开发一个 SSL TCP 服务器,并针对多线程客户端对其进行测试。当客户端线程数小于190时,服务端没有问题,所有的消息都被正确接收。但是一旦我将客户端的线程数增加到 195 以上,就会出现两个问题:

问题 1:服务器端异常 ECONNABORTED

/usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/openssl/ssl.rb:232:in `accept': Software caused connection abort - accept(2) (Errno::ECONNABORTED)
from /usr/local/rvm/rubies/ruby-2.1.5/lib/ruby/2.1.0/openssl/ssl.rb:232:in `accept'
from server.rb:30:in `block (2 levels) in start_server'

我可以通过在异常处理程序中重新启动接受循环来解决这个问题。

问题 2:服务器卡住当我增加客户端的线程数(例如 250)时,几秒钟后,服务器卡住,即没有异常,也没有新的连接被允许。这真的很烦人,因为服务器端无法知道它已被卡住。

操作系统:FreeBSD 10.1Ruby 版本:2.2.1(也试过 2.1.5)


服务器代码:

loop do
server = TCPServer.new(ip_address, port)
sslContext = OpenSSL::SSL::SSLContext.new
sslContext.cert = OpenSSL::X509::Certificate.new(File.open("cert/cert.pem"))
sslContext.key = OpenSSL::PKey::RSA.new(File.open("cert/key.pem"), SSL_PASSWORD)
sslServer = OpenSSL::SSL::SSLServer.new(server, sslContext)

loop do
Thread.new(sslServer.accept) do |connection|
begin
messageIn = connection.gets
connection.close
rescue Exception => ex
puts "Exception in main loop : " + ex.message
puts "Backtrace : " + ex.backtrace.join("\n")
end
end
end
end
end

客户端代码:

def create_client(host, port)
begin
socket = TCPSocket.open(host,port)
ssl_context = OpenSSL::SSL::SSLContext.new()
ssl_context.cert = OpenSSL::X509::Certificate.new(File.open("lib/cert/cert.pem"))
ssl_context.key = OpenSSL::PKey::RSA.new(File.open("lib/cert/key.pem"), SSL_PASSWORD)
ssl_context.ssl_version = :SSLv3
ssl_context.ssl_timeout = 10
ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
ssl_socket.sync_close = true
ssl_socket.connect
rescue Exception => ex
puts "Exception in create_client"
sleep 1
return create_client(host, port )
end

return ssl_socket
end

........

for j in 1..10 do
threads = []
for i in 1..n.to_i do
threads << Thread.new do
begin
socket = create_client(ip, port)
socket.puts("hello")
socket.flush
socket.close
rescue Exception => ex
puts "Exception"
end
end
end

threads.each(&:join)
end

最佳答案

听起来您在服务器端遇到了网络限制。您可能需要使用 sysctl kern.ipc.soacceptqueue=250 增加队列长度(或在/etc/sysctl.conf 中永久设置)。您可以使用 netstat -Lan 检查应用程序连接队列。

关于当超过 250 个并发连接时,Ruby SSL TCP 服务器卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30979400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com