Permalink
Browse files

Use send/receive timeouts on the amqp socket and the system_timer gem…

… (if available)
  • Loading branch information...
1 parent 5106973 commit f43d6681587fe26d6102d5a8d4fc81d850632537 @paukul committed Nov 15, 2010
Showing with 63 additions and 2 deletions.
  1. +15 −0 amqp_logging.gemspec
  2. +4 −2 lib/amqp_logging.rb
  3. +44 −0 lib/amqp_logging/ext/qrack/client.rb
View
@@ -20,6 +20,21 @@ Gem::Specification.new do |s|
s.summary = "A ruby logger class that logs to an AMQP exchange in addition to your default log device."
s.test_files = Dir['test/**/*.rb']
+ s.post_install_message = <<-INFO
+ *********************************************************************************************
+
+ If you're running a ruby version < 1.9 please install the system_timer gem because rubys
+ default timeout.rb isn't reliable.
+
+ "In particular, it is guaranteed to not work in Ruby M.R.I 1.8 when you are issuing
+ system calls that can cause your Ruby process to sleep for a period of time that is
+ longer than your timeout."
+
+ See: http://ph7spot.com/musings/system-timer
+
+ *********************************************************************************************
+ INFO
+
s.specification_version = 3
s.add_runtime_dependency("bunny", [">= 0.6.0"])
s.add_runtime_dependency("activesupport", [">= 2.3.0"])
View
@@ -3,16 +3,17 @@
begin
# ActiveSupport 3.x
require 'active_support/time'
+ require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/hash/slice'
rescue LoadError
- require 'active_support' # ActiveSupport 2.x
+ # ActiveSupport 2.x
+ require 'active_support'
end
module AMQPLogging
autoload :MetricsAgent, 'amqp_logging/metrics_agent'
private
- def self.iso_time_with_nanoseconds(t = Time.now)
def self.iso_time_with_microseconds(t = Time.now)
t.strftime("%Y-%m-%dT%H:%M:%S.#{t.usec}")
end
@@ -21,3 +22,4 @@ def self.iso_time_with_microseconds(t = Time.now)
require 'logger'
require 'amqp_logging/logger'
require 'amqp_logging/log_device'
+require 'amqp_logging/ext/qrack/client'
@@ -0,0 +1,44 @@
+require 'qrack/client'
+
+
+unless Qrack::Client.instance_methods.include?(:socket_with_reliable_timeout)
+ module AMQPLogging
+ Timer = if RUBY_VERSION < "1.9"
+ begin
+ require 'system_timer'
+ SystemTimer
+ rescue LoadError
+ require 'timeout'
+ Timeout
+ end
+ else
+ require 'timeout'
+ Timeout
+ end
+ end
+
+ module Qrack
+ class Client
+ # overwrite the timeout method so that SystemTimer is used
+ # instead the standard timeout.rb: http://ph7spot.com/musings/system-timer
+ delegate :timeout, :to => AMQPLogging::Timer
+
+ def socket_with_reliable_timeout
+ socket_without_reliable_timeout
+
+ secs = Integer(CONNECT_TIMEOUT)
+ usecs = Integer((CONNECT_TIMEOUT - secs) * 1_000_000)
+ optval = [secs, usecs].pack("l_2")
+
+ begin
+ @socket.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
+ @socket.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
+ rescue Errno::ENOPROTOOPT
+ end
+ @socket
+ end
+ alias_method_chain :socket, :reliable_timeout
+
+ end
+ end
+end

0 comments on commit f43d668

Please sign in to comment.