Module: Arborist::Monitor::ConnectionBatching

Included in:
Socket::TCP, Socket::UDP
Defined in:
lib/arborist/monitor/connection_batching.rb

Overview

A mixin for adding batched connections for socket-based monitors.

Defined Under Namespace

Classes: BatchRunner

Constant Summary collapse

DEFAULT_BATCH_SIZE =

The default number of connections to have open – this should be well under the RLIMIT_NOFILE of the current process.

150
DEFAULT_TIMEOUT =

The default connection timeout

2.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object

Inclusion callback – add the #batchsize attribute to including monitors.



225
226
227
228
229
230
# File 'lib/arborist/monitor/connection_batching.rb', line 225

def self::included( mod )
	mod.attr_accessor :timeout
	mod.attr_accessor :batch_size

	super
end

Instance Method Details

#handle_connections(connections_enum) ⇒ Object

Fetch connections from connections_enum and build a Hash of node updates keyed by identifier based on the results.



283
284
285
286
287
288
289
# File 'lib/arborist/monitor/connection_batching.rb', line 283

def handle_connections( connections_enum )
	runner = BatchRunner.new( connections_enum, self.batch_size, self.timeout )
	runner.run do |conn_hash, duration|
		self.status_for_conn( conn_hash, duration )
	end
	return runner.results
end

#make_connections_enum(nodes) ⇒ Object

Return an Enumerator that yields Hashes that describe the connections to be made. They must contain, at a minimum, the following keys:

conn

The Socket (or other IO object) that is used to communicate with the monitored host. This should be created using non-blocking connection.

identifier

The node identifier associated with the conn.

You can add any other members to each Hash that you require to actually use the connection when it becomes available.



267
268
269
# File 'lib/arborist/monitor/connection_batching.rb', line 267

def make_connections_enum( nodes )
	raise "%p does not provide a %s method!" % [ __method__ ]
end

#run(nodes) ⇒ Object

Run the monitor, batching connections for the specified nodes so the monitor doesn’t exhaust its file descriptors.



252
253
254
255
# File 'lib/arborist/monitor/connection_batching.rb', line 252

def run( nodes )
	connections = self.make_connections_enum( nodes )
	return self.handle_connections( connections )
end

#status_for_conn(conn_hash, duration) ⇒ Object

Called when a socket becomes ready. It should generate a status update for the node that corresponds to the given node_hash and return it as a Hash. The duration is how long it took for the connection to be ready, in seconds.



276
277
278
# File 'lib/arborist/monitor/connection_batching.rb', line 276

def status_for_conn( conn_hash, duration )
	raise "%p does not provide a %s method!" % [ __method__ ]
end

#with_batch_size(new_size) ⇒ Object

Return a clone of the receiving monitor with its batch size set to new_size.



235
236
237
238
239
# File 'lib/arborist/monitor/connection_batching.rb', line 235

def with_batch_size( new_size )
	copy = self.clone
	copy.batch_size = new_size
	return copy
end

#with_timeout(new_timeout) ⇒ Object

Return a clone of receiving monitor with its timeout set to new_timeout.



243
244
245
246
247
# File 'lib/arborist/monitor/connection_batching.rb', line 243

def with_timeout( new_timeout )
	copy = self.clone
	copy.timeout = new_timeout
	return copy
end