public
Created

Demo and workaround for post-connection delay bug in EventMachine 1.0.0

  • Download Gist
bug.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#!/usr/bin/env ruby
#
# Demo and workaround for post-connection delay bug in EventMachine 1.0.0
#
# See https://github.com/eventmachine/eventmachine/issues/374 for explanation.
 
require 'rubygems'
require 'eventmachine' # 1.0.0
require 'amqp' # 0.8.0 / 54f18d426
 
# Configure AMQP, and capture async notification of connection status in
# configuration to survive reconnects.
 
::AMQP.settings.merge!({
:host => "localhost", :port => 5672, :user => "guest", :pass => "guest", :vhost => "/",
:connection_status => proc do |status|
@channel = ::AMQP::Channel.new(::AMQP.connection) if status == :connected
end
})
 
#EventMachine.epoll = true # exhibits same problem as select
#EventMachine.kqueue = true # doesn't suffer
 
##
## Initiate AMQP connection and wait for it to complete before proceeding.
##
 
@mutex = Mutex.new
@cv = ConditionVariable.new
 
Thread.new do
::AMQP.start do |c|
# Fix is to uncomment the following. Not sure if this survives
# reconnects:
#c.pending_connect_timeout = 0.1
@cv.signal
end
end
 
@mutex.synchronize { @cv.wait(@mutex) }
 
##
## Watch wireline packets to observe problem (e.g. ngrep -Td lo0 port 5672).
## The first will go out, the second will delay.
##
 
loop do
@channel.queue("some_queue", :durable => true).publish("some msg")
STDIN.readline
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.