lib/m2r/connection.rb in m2r-2.0.2 vs lib/m2r/connection.rb in m2r-2.1.0.pre
- old
+ new
@@ -1,11 +1,16 @@
require 'm2r'
module M2R
# Connection for exchanging data with mongrel2
class Connection
- class Error < StandardError; end
+ class Error < StandardError
+ attr_accessor :errno
+ def signal?
+ errno == ZMQ::EINTR
+ end
+ end
# @param [ZMQ::Socket] request_socket socket for receiving requests
# from Mongrel2
# @param [ZMQ::Socket] response_socket socket for sending responses
# to Mongrel2
@@ -28,11 +33,15 @@
# @note This is blocking call
# @return [String] M2 request message
# @api public
def receive
ret = @request_socket.recv_string(msg = "")
- raise Error, "Unable to receive message: #{ZMQ::Util.error_string}" if ret < 0
+ if ret < 0
+ e = Error.new "Unable to receive message: #{ZMQ::Util.error_string}"
+ e.errno = ZMQ::Util.errno
+ raise e
+ end
return msg
end
# Sends response to Mongrel2 for given request
#
@@ -52,14 +61,30 @@
# @param [Array<String>, String] connection_ids Mongrel2 connections ids
# @param [String] data Data that should be delivered to the connections
# @return [String] M2 response message
#
# @api public
- def deliver(uuid, connection_ids, data)
+ def deliver(uuid, connection_ids, data, trial = 1)
msg = "#{uuid} #{TNetstring.dump([*connection_ids].join(' '))} #{data}"
- ret = @response_socket.send_string(msg, ZMQ::NOBLOCK)
- raise Error, "Unable to deliver message: #{ZMQ::Util.error_string}" if ret < 0
+ ret = @response_socket.send_string(msg, ZMQ::NonBlocking)
+ if ret < 0
+ e = Error.new "Unable to deliver message: #{ZMQ::Util.error_string}"
+ e.errno = ZMQ::Util.errno
+ raise e
+ end
return msg
+ rescue Connection::Error => er
+ raise if trial >= 3
+ raise unless er.signal?
+ deliver(uuid, connection_ids, data, trial + 1)
+ end
+
+ # Closes ZMQ sockets
+ #
+ # @api public
+ def close
+ @request_socket.close
+ @response_socket.close
end
private
def close?(request, response_or_string)