tags:

views:

111

answers:

1

Hi,

I have some fairly boiler-plate Ruby code (running on Linux) which sends a GET request to a server ...

req = Net::HTTP::Get.new(path)
req.content_type = 'text/plain; charset=utf-8'
req.body = ''

port = 443

res = Net::HTTP.new($host, port)

res.use_ssl = true

res.start do |http|
    t = Benchmark.measure do
        _return = http.request(req).body
    end

    $time= t.real
end

req = nil
res = nil

The problem I'm having is that when I call this code in a tight loop, I eventually fill the system up with sockets in the TIME_WAIT state (48687 at last count).

I guess there's nothing special about Ruby here, I'd run into the same problem with C, but is there any GC related problem here? Any tips or tricks for preventing this from happening?

+1  A: 

Not much you can do about it. Take a look at http://serverfault.com/questions/86550/apache-keep-alive-or-not-keep-alive/86565#86565. There's a way of reducing the time sockets stay in that state, but reducing it is "dangerous". The best you can do is to try reusing the connection if the server keeps it open.

Gonzalo
Thanks. As an old C programmer I was happy why it was happening, just wondered if there were any tricks in the Ruby lib for helping with the problem.
Chris McCauley