为什么有这么多“TIME_WAIT"?使用 EventMachine 时连接? [英] Why are there so many "TIME_WAIT" connectings when using EventMachine?
本文介绍了为什么有这么多“TIME_WAIT"?使用 EventMachine 时连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我为 EventMachine 运行我的测试代码时,我发现有太多的TIME_WAIT
"连接.有问题吗?
我的测试客户端代码是:
需要'rubygems'需要基准"需要插座"需要记录器"Benchmark.bm 做 |x|logger = Logger.new('test.log', 10, 1024000)logger.datetime_format = "%Y-%m-%d %H:%M:%S"logger.info "-----------------------------------"x.report("times:") 做因为我在 0..20睡眠 0.1Thread.new 做TCPSocket.open "127.0.0.1", 8080 do |s|s.send "#{i}", 0如果结果 = s.recv(100)logger.info 结果结尾结尾结尾结尾结尾结尾
我的测试服务器代码是:
需要'rubygems'需要基准"需要事件机器"类处理程序<事件机器::连接定义初始化(*参数)极好的结尾定义接收数据(数据)把------------------------"@reply = 数据@state = :处理EventMachine.defer(method(:do_something), method(:callback))救援异常 =>前任LOGGER.error "#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n")}"结尾def do_something#模拟长时间运行的请求a = []因为我在 1..3000<<兰特(3000)a.排序!结尾返回@reply结尾定义回调(味精)self.send_data msg@state = :关闭结尾解绑close_connection_after_writing #除非@state == :processing结尾结尾事件机器::运行{事件机器.epollEventMachine::start_server("0.0.0.0", 8080, Handler)把听着……"}
解决方案
TIME_WAIT 状态持续两分钟,并在 TCP 中提供极其重要的连接安全性.这不是问题,而是解决方案.
When I run my test code for EventMachine, I found there are too many "TIME_WAIT
" connections. Is it a problem?
run netstat -anp | grep 8080 tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 13012/ruby tcp 0 0 127.0.0.1:38701 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38706 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38709 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38708 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38699 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38700 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38707 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38705 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38702 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38703 127.0.0.1:8080 TIME_WAIT - tcp 0 0 127.0.0.1:38704 127.0.0.1:8080 TIME_WAIT -
My test client code is:
require 'rubygems'
require 'benchmark'
require 'socket'
require 'logger'
Benchmark.bm do |x|
logger = Logger.new('test.log', 10, 1024000)
logger.datetime_format = "%Y-%m-%d %H:%M:%S"
logger.info "----------------------------------"
x.report("times:") do
for i in 0..20
sleep 0.1
Thread.new do
TCPSocket.open "127.0.0.1", 8080 do |s|
s.send "#{i}", 0
if result = s.recv(100)
logger.info result
end
end
end
end
end
end
My test server code is:
require 'rubygems'
require 'benchmark'
require 'eventmachine'
class Handler < EventMachine::Connection
def initialize(*args)
super
end
def receive_data(data)
puts "------------------------"
@reply = data
@state = :processing
EventMachine.defer(method(:do_something), method(:callback))
rescue Exception => ex
LOGGER.error "#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n")}"
end
def do_something
#simulate a long running request
a = []
for i in 1..3000
a << rand(3000)
a.sort!
end
return @reply
end
def callback(msg)
self.send_data msg
@state = :closing
end
def unbind
close_connection_after_writing #unless @state == :processing
end
end
EventMachine::run {
EventMachine.epoll
EventMachine::start_server("0.0.0.0", 8080, Handler)
puts "Listening..."
}
解决方案
The TIME_WAIT state lasts for two minutes and provides critically important connection security in TCP. It isn't a problem, it's a solution.
这篇关于为什么有这么多“TIME_WAIT"?使用 EventMachine 时连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文