Class: Arborist::Monitor::Socket::UDP

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Includes:
ConnectionBatching
Defined in:
lib/arborist/monitor/socket.rb

Overview

Arborist UDP socket monitor logic

Constant Summary collapse

USED_PROPERTIES =

Always request the node addresses and port.

[ :addresses, :port ].freeze

Constants included from ConnectionBatching

ConnectionBatching::DEFAULT_BATCH_SIZE, ConnectionBatching::DEFAULT_TIMEOUT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConnectionBatching

#handle_connections, included, #run, #with_batch_size, #with_timeout

Constructor Details

#initialize(timeout: Arborist::Monitor::Socket.default_timeout, batch_size: Arborist::Monitor::Socket.batch_size) ⇒ UDP

Create a new UDP monitor with the specified options. Valid options are:

:timeout

Set the number of seconds to wait for a connection for each node.

:batch_size

The number of UDP connection attempts to perform simultaneously.


160
161
162
163
# File 'lib/arborist/monitor/socket.rb', line 160

def initialize( timeout: Arborist::Monitor::Socket.default_timeout, batch_size: Arborist::Monitor::Socket.batch_size )
	self.timeout = timeout
	self.batch_size = batch_size
end

Class Method Details

.node_propertiesObject

Return the properties used by this monitor.



149
150
151
# File 'lib/arborist/monitor/socket.rb', line 149

def self::node_properties
	return USED_PROPERTIES
end

.run(nodes) ⇒ Object

Instantiate a monitor check and run it for the specified nodes.



143
144
145
# File 'lib/arborist/monitor/socket.rb', line 143

def self::run( nodes )
	return self.new.run( nodes )
end

Instance Method Details

#make_connections_enum(nodes) ⇒ Object

Open a socket for each of the specified nodes and return a Hash of the sockets (or the error from the connection attempt) keyed by node identifier.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/arborist/monitor/socket.rb', line 173

def make_connections_enum( nodes )
	return nodes.lazy.map do |identifier, node_data|
		address = node_data['addresses'].first
		port = node_data['port']

		self.log.debug "Creating UDP connection for %s:%d" % [ address, port ]
		sock = Socket.new( :INET, :DGRAM )

		conn = begin
				sockaddr = Socket.sockaddr_in( port, address )
				sock.connect( sockaddr )
				sock.send( '', 0 )
				sock.recvfrom_nonblock( 1 )
				sock
			rescue Errno::EAGAIN
				self.log.debug "  connection started"
				sock
			rescue => err
				self.log.error "  %p setting up connection: %s" % [ err.class, err.message ]
				err
			end

		self.log.debug "UDP connection object is: %p" % [ conn ]
		{ conn: conn, identifier: identifier }
	end
end

#status_for_conn(conn_hash, duration) ⇒ Object

Build a status for the specified conn_hash after its :conn has indicated it is ready.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/arborist/monitor/socket.rb', line 203

def status_for_conn( conn_hash, duration )
	sock = conn_hash[:conn]
	sock.recvfrom_nonblock( 1 )
	return {
		udp_socket_connect: { duration: duration }
	}
rescue Errno::EAGAIN
	return {
		udp_socket_connect: { duration: duration }
	}
rescue SocketError, SystemCallError => err
	self.log.debug "Got %p while connecting to %s" % [ err.class, conn_hash[:identifier] ]
	return { error: err.message }
ensure
	sock.close if sock
end