eitoball / net-ping

A collection of classes that provide different ways to ping computers.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not threadsafe?

peterstory opened this issue · comments

I've run into problems using Net::Ping::External with threads.

  Thread.new host do |host|
    # Get relevant variables
    host_name = host["host_name"]
    metric_name = host["metric_name"]

    # Ping the host, with a timeout of 5 seconds
    pinger = Net::Ping::External.new(host_name)
    pinger.timeout = 5
    success = pinger.ping
    if success
      time_taken = pinger.duration
    else
      time_taken = pinger.timeout
    end

    # Record the time taken in statsd
    puts "#{host_name}: #{time_taken}"
  end

In this example, I create two threads, to ping 2 different hosts:

10.11.1.1: 0.007786
10.11.1.2: 0.007786
*10 second pause I added*
10.11.1.1: 
10.11.1.2: 0.018783
*10 second pause I added*
10.11.1.1: 0.014204
10.11.1.2: 0.014204

It seems that some data structure is being shared by the 2 threads.

Whoops, answered my own question. The pinger variable shared the scope with the Thread caller, which meant that each thread was overwriting pinger.