Browse files

use EM reactor if used within event loop

  • Loading branch information...
1 parent 4fed744 commit 161a3c70708cddcbe0818b7fef3fd8177d11155f @igrigorik committed Jul 29, 2010
Showing with 33 additions and 4 deletions.
  1. +14 −4 lib/gmetric.rb
  2. +19 −0 spec/gmetric_spec.rb
View
18 lib/gmetric.rb
@@ -13,12 +13,22 @@ class GMetric
}
def self.send(host, port, metric)
- conn = UDPSocket.new
- conn.connect(host, port)
gmetric = self.pack(metric)
- conn.send gmetric[0], 0
- conn.send gmetric[1], 0
+ if defined?(EventMachine) and EventMachine.reactor_running?
+ # open an ephemereal UDP socket since
+ # we do not plan on recieving any data
+ conn = EM.open_datagram_socket('', 0)
+
+ conn.send_datagram gmetric[0], host, port
+ conn.send_datagram gmetric[1], host, port
+ else
+ conn = UDPSocket.new
+ conn.connect(host, port)
+
+ conn.send gmetric[0], 0
+ conn.send gmetric[1], 0
+ end
end
def self.pack(metric)
View
19 spec/gmetric_spec.rb
@@ -84,4 +84,23 @@ def hex(data)
}.should_not raise_error
end
+
+ it "should use EM reactor if used within event loop" do
+ pending 'stub out connection class'
+
+ require 'rubygems'
+ require 'eventmachine'
+ EventMachine.run do
+ Ganglia::GMetric.send("127.0.0.1", 1111, {
+ :name => 'pageviews',
+ :units => 'req/min',
+ :type => 'uint8',
+ :value => 7000,
+ :tmax => 60,
+ :dmax => 300
+ })
+
+ EM.stop
+ end
+ end
end

0 comments on commit 161a3c7

Please sign in to comment.