Skip to content

Instantly share code, notes, and snippets.

@jpr5
Created October 16, 2012 20:28
Show Gist options
  • Save jpr5/3901782 to your computer and use it in GitHub Desktop.
Save jpr5/3901782 to your computer and use it in GitHub Desktop.
Demo and workaround for post-connection delay bug in EventMachine 1.0.0
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment