Created
February 16, 2011 19:21
-
-
Save chuckremes/829967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'zmqmachine' | |
# This example illustrates how to make 0mq run out of | |
# mailbox space and assert at mailbox.cpp line 182 | |
# | |
# This code sets up a ping pong between two classes. The PingHandler | |
# class initiates the volley using a REQ socket. The PongHandler | |
# class returns the volley with a REP socket. | |
# | |
# Here's the wrinkle. Set up two PongHandlers so that it load | |
# balances between them. To cause the crash, set the socket | |
# identity for both PongHandlers to the same value. | |
# | |
class PongHandler | |
def on_attach socket | |
socket.identity = 'the_same' # causes thousands of disconnect/reconnects and a crash! | |
address = ZM::Address.new '127.0.0.1', 5555, :tcp | |
rc = socket.connect address | |
end | |
def on_writable socket | |
nil | |
end | |
def on_readable socket, messages | |
socket.send_messages messages | |
end | |
end | |
class PingHandler | |
def initialize context | |
@context = context | |
end | |
def on_attach socket | |
@context.register_readable socket | |
address = ZM::Address.new '127.0.0.1', 5555, :tcp | |
rc = socket.bind address | |
@socket = socket | |
end | |
def start | |
rc = @socket.send_message_string "#{'a' * 2048}" | |
@sent_count += 1 | |
end | |
def on_readable socket, messages | |
rc = socket.send_messages messages | |
end | |
end | |
# Run both handlers within the same reactor context | |
ctx1 = ZM::Reactor.new(:test).run do |context| | |
@ping_handler = PingHandler.new context | |
context.req_socket @ping_handler | |
@pong_handler1 = PongHandler.new context | |
@pong_handler2 = PongHandler.new context | |
context.rep_socket @pong_handler1 | |
context.rep_socket @pong_handler2 | |
# give the sockets 10ms to bind & connect before | |
# sending the first message | |
context.oneshot_timer(10) { @ping_handler.start } | |
end | |
ctx1.join 5_000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment