Created
June 4, 2011 12:44
-
-
Save hryk/1007869 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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'ffi-rzmq' | |
REQUEST_RETRIES = 3 | |
REQUEST_TIMEOUT = 2500 | |
REQUEST_ENDPOINT = "tcp://127.0.0.1:5555" | |
module ZMQ | |
class Poller | |
def items | |
@items | |
end | |
end | |
end | |
context = ZMQ::Context.new 1 | |
socket = context.socket ZMQ::REQ | |
socket.connect REQUEST_ENDPOINT | |
poller = ZMQ::Poller.new | |
poller.register socket, ZMQ::POLLIN | |
sequence = 0 | |
retries_left = REQUEST_RETRIES | |
while retries_left > 0 do | |
sequence += 1 | |
request = "request #{sequence}" | |
socket.send_string request | |
expect_reply = true | |
while expect_reply do | |
items = poller.items | |
rc = nil | |
begin | |
t0 = Time.now | |
poller.poll REQUEST_TIMEOUT | |
puts "#{(Time.now - t0).to_f} : polling" | |
rescue ZMQ::PollError => e | |
puts e.message | |
exit | |
end | |
if (poller.items.get(0).readable?) | |
reply = socket.recv_string | |
break if reply.nil? | |
if (reply == request) | |
puts "I: server replied OK (#{reply})" | |
expect_reply = false | |
else | |
puts "E: malformed reply from server" | |
end | |
else | |
retries_left -= 1 | |
if retries_left == 0 | |
puts "E: server seems to be offline, abandoning" | |
socket.close | |
exit | |
break | |
else | |
puts "W: no response from server, retrying..." | |
poller.deregister socket, ZMQ::POLLIN | |
socket.close | |
socket = nil | |
puts "I: reconnecting to server" | |
socket = context.socket ZMQ::REQ | |
socket.connect REQUEST_ENDPOINT | |
poller.register socket, ZMQ::POLLIN | |
socket.send_string request | |
end | |
end | |
end | |
end |
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'ffi-rzmq' | |
context = ZMQ::Context.new 1 | |
socket = context.socket ZMQ::REP | |
socket.bind "tcp://127.0.0.1:5555" | |
cycles = 0 | |
loop do | |
request = socket.recv_string | |
cycles += 1 | |
if (cycles > 3 && rand(3) == 0) | |
puts "I: simulating a crash" | |
break | |
else | |
if (cycles > 3 && rand(3) == 0) | |
puts "I: simulating CPU overload" | |
sleep 2 | |
end | |
puts "I: normal request (#{request})" | |
sleep 1 # do something... | |
socket.send_string(request) | |
end | |
end | |
socket.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment