Class: Riemann::Client::UDP

Inherits:
Riemann::Client show all
Defined in:
lib/riemann/client/udp.rb

Constant Summary collapse

MAX_SIZE =
16384

Constants inherited from Riemann::Client

HOST, PORT, TIMEOUT

Instance Attribute Summary collapse

Attributes inherited from Riemann::Client

#tcp, #timeout, #udp

Instance Method Summary collapse

Methods inherited from Riemann::Client

#<<, #[], #query

Constructor Details

#initialize(opts = {}) ⇒ UDP

Returns a new instance of UDP.



8
9
10
11
12
13
# File 'lib/riemann/client/udp.rb', line 8

def initialize(opts = {})
  @host = opts[:host] || HOST
  @port = opts[:port] || PORT
  @max_size = opts[:max_size] || MAX_SIZE
  @locket = Mutex.new
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/riemann/client/udp.rb', line 6

def host
  @host
end

#max_sizeObject

Returns the value of attribute max_size.



6
7
8
# File 'lib/riemann/client/udp.rb', line 6

def max_size
  @max_size
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/riemann/client/udp.rb', line 6

def port
  @port
end

#socketObject

Returns the value of attribute socket.



6
7
8
# File 'lib/riemann/client/udp.rb', line 6

def socket
  @socket
end

Instance Method Details

#closeObject



19
20
21
22
23
# File 'lib/riemann/client/udp.rb', line 19

def close
  @locket.synchronize do
    @socket.close
  end
end

#connectObject



15
16
17
# File 'lib/riemann/client/udp.rb', line 15

def connect
  @socket = UDPSocket.new
end

#connected?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/riemann/client/udp.rb', line 25

def connected?
  !!@socket && @socket.closed?
end

#read_message(s) ⇒ Object

Read a message from a stream

Raises:



30
31
32
# File 'lib/riemann/client/udp.rb', line 30

def read_message(s)
  raise Unsupported
end

#send_maybe_recv(message) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/riemann/client/udp.rb', line 38

def send_maybe_recv(message)
  with_connection do |s|
    x = message.encode ''
    unless x.length < @max_size
      raise TooBig
    end

    s.send(x, 0, @host, @port)
    nil
  end
end

#send_recv(*a) ⇒ Object

Raises:



34
35
36
# File 'lib/riemann/client/udp.rb', line 34

def send_recv(*a)
  raise Unsupported
end

#with_connectionObject

Yields a connection in the block.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/riemann/client/udp.rb', line 51

def with_connection
  tries = 0
  
  @locket.synchronize do
    begin
      tries += 1
        yield(@socket || connect)
    rescue IOError => e
      raise if tries > 3
      connect and retry
    rescue Errno::EPIPE => e
      raise if tries > 3
      connect and retry
    rescue Errno::ECONNREFUSED => e
      raise if tries > 3
      connect and retry
    rescue Errno::ECONNRESET => e
      raise if tries > 3
      connect and retry
    rescue InvalidResponse => e
      raise if tries > 3
      connect and retry
    end
  end
end