class Riemann::Client::TCP
Attributes
socket_factory[W]
host[RW]
port[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/riemann/client/tcp.rb, line 29 def initialize(options = {}) # rubocop:disable Lint/MissingSuper @options = options @locket = Monitor.new @socket = nil @pid = nil end
socket_factory()
click to toggle source
Public: Return a socket factory
# File lib/riemann/client/tcp.rb, line 19 def self.socket_factory @socket_factory ||= proc { |options| if options[:ssl] SSLSocket.connect(options) else TcpSocket.connect(options) end } end
Public Instance Methods
close()
click to toggle source
# File lib/riemann/client/tcp.rb, line 49 def close @locket.synchronize do @socket.close if connected? @socket = nil end end
connected?()
click to toggle source
# File lib/riemann/client/tcp.rb, line 56 def connected? @locket.synchronize do !@socket.nil? && !@socket.closed? end end
read_message(socket)
click to toggle source
Read a message from a stream
# File lib/riemann/client/tcp.rb, line 63 def read_message(socket) unless (buffer = socket.read(4)) && (buffer.size == 4) raise InvalidResponse, 'unexpected EOF' end length = buffer.unpack1('N') begin str = socket.read length message = Riemann::Message.decode str rescue StandardError puts "Message was #{str.inspect}" raise end unless message.ok puts 'Failed' raise ServerError, message.error end message end
send_recv(message)
click to toggle source
# File lib/riemann/client/tcp.rb, line 85 def send_recv(message) with_connection do |socket| socket.write(message.encode_with_length) read_message(socket) end end
Also aliased as: send_maybe_recv
socket()
click to toggle source
# File lib/riemann/client/tcp.rb, line 36 def socket @locket.synchronize do close if @pid && @pid != Process.pid return @socket if connected? @socket = self.class.socket_factory.call(@options) @pid = Process.pid return @socket end end
with_connection() { |socket| ... }
click to toggle source
Yields a connection in the block.
# File lib/riemann/client/tcp.rb, line 95 def with_connection tries = 0 @locket.synchronize do tries += 1 yield(socket) rescue IOError, Errno::EPIPE, Errno::ECONNREFUSED, InvalidResponse, Timeout::Error, Riemann::Client::TcpSocket::Error close raise if tries > 3 retry rescue StandardError close raise end end