Why are there so many “TIME_WAIT” connectings when using EventMachine?

梦想的初衷 提交于 2019-12-11 04:33:32

问题


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..."
}

回答1:


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.



来源:https://stackoverflow.com/questions/8633677/why-are-there-so-many-time-wait-connectings-when-using-eventmachine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!