Class: Riemann::Client::TCP
- Inherits:
-
Riemann::Client
- Object
- Riemann::Client
- Riemann::Client::TCP
- Defined in:
- lib/riemann/client/tcp.rb
Constant Summary
Constants inherited from Riemann::Client
Class Attribute Summary collapse
-
.socket_factory ⇒ Object
Public: Return a socket factory.
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Attributes inherited from Riemann::Client
Instance Method Summary collapse
- #close ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(options = {}) ⇒ TCP
constructor
rubocop:disable Lint/MissingSuper.
-
#read_message(socket) ⇒ Object
Read a message from a stream.
- #send_recv(message) ⇒ Object (also: #send_maybe_recv)
- #socket ⇒ Object
-
#with_connection ⇒ Object
Yields a connection in the block.
Methods inherited from Riemann::Client
#<<, #[], #bulk_send, #connect, #normalize_events, #query, #timeout
Constructor Details
#initialize(options = {}) ⇒ TCP
rubocop:disable Lint/MissingSuper
29 30 31 32 33 34 |
# File 'lib/riemann/client/tcp.rb', line 29 def initialize( = {}) # rubocop:disable Lint/MissingSuper @options = @locket = Monitor.new @socket = nil @pid = nil end |
Class Attribute Details
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/riemann/client/tcp.rb', line 10 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/riemann/client/tcp.rb', line 10 def port @port end |
Instance Method Details
#close ⇒ Object
49 50 51 52 53 54 |
# File 'lib/riemann/client/tcp.rb', line 49 def close @locket.synchronize do @socket.close if connected? @socket = nil end end |
#connected? ⇒ Boolean
56 57 58 59 60 |
# File 'lib/riemann/client/tcp.rb', line 56 def connected? @locket.synchronize do !@socket.nil? && !@socket.closed? end end |
#read_message(socket) ⇒ Object
Read a message from a stream
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/riemann/client/tcp.rb', line 63 def (socket) unless (buffer = socket.read(4)) && (buffer.size == 4) raise InvalidResponse, 'unexpected EOF' end length = buffer.unpack1('N') begin str = socket.read length = Riemann::Message.decode str rescue StandardError puts "Message was #{str.inspect}" raise end unless .ok puts 'Failed' raise ServerError, .error end end |
#send_recv(message) ⇒ Object Also known as: send_maybe_recv
85 86 87 88 89 90 |
# File 'lib/riemann/client/tcp.rb', line 85 def send_recv() with_connection do |socket| socket.write(.encode_with_length) (socket) end end |
#socket ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 ⇒ Object
Yields a connection in the block.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# 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 |