lib/bertrpc/action.rb in bertrpc-1.0.0 vs lib/bertrpc/action.rb in bertrpc-1.1.0
- old
+ new
@@ -22,11 +22,11 @@
sock.write([bert.length].pack("N"))
sock.write(bert)
end
def transaction(bert_request)
- sock = TCPSocket.new(@svc.host, @svc.port)
+ sock = connect_to(@svc.host, @svc.port, @svc.timeout)
if @req.options
if @req.options[:cache] && @req.options[:cache][0] == :validation
token = @req.options[:cache][1]
info_bert = encode_ruby_request([:info, :cache, [:validation, token]])
@@ -42,8 +42,24 @@
raise ProtocolError.new(ProtocolError::NO_DATA) unless bert_response
sock.close
bert_response
rescue Errno::ECONNREFUSED
raise ConnectionError.new("Unable to connect to #{@svc.host}:#{@svc.port}")
+ rescue Timeout::Error
+ raise ReadTimeoutError.new("No response from #{@svc.host}:#{@svc.port} in #{@svc.timeout}s")
end
+
+ # Creates a socket object which does speedy, non-blocking reads
+ # and can perform reliable read timeouts.
+ #
+ # Raises Timeout::Error on timeout.
+ #
+ # +host+ String address of the target TCP server
+ # +port+ Integer port of the target TCP server
+ # +timeout+ Optional Integer (in seconds) of the read timeout
+ def connect_to(host, port, timeout = nil)
+ io = BufferedIO.new(TCPSocket.new(host, port))
+ io.read_timeout = timeout
+ io
+ end
end
-end
\ No newline at end of file
+end