A mixin for adding batched connections for socket-based monitors.
The default number of connections to have open – this should be well under the RLIMIT_NOFILE of the current process.
The default connection timeout
Inclusion callback – add the batchsize attribute to including monitors.
# File lib/arborist/monitor/connection_batching.rb, line 224
def self::included( mod )
mod.attr_accessor :timeout
mod.attr_accessor :batch_size
super
end
Fetch connections from connections_enum
and build a Hash of
node updates keyed by identifier based on the results.
# File lib/arborist/monitor/connection_batching.rb, line 282
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
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.
# File lib/arborist/monitor/connection_batching.rb, line 266
def make_connections_enum( nodes )
raise "%p does not provide a %s method!" % [ __method__ ]
end
Run the monitor, batching connections for the specified nodes
so the monitor doesn't exhaust its file descriptors.
# File lib/arborist/monitor/connection_batching.rb, line 251
def run( nodes )
connections = self.make_connections_enum( nodes )
return self.handle_connections( connections )
end
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.
# File lib/arborist/monitor/connection_batching.rb, line 275
def status_for_conn( conn_hash, duration )
raise "%p does not provide a %s method!" % [ __method__ ]
end
Return a clone of the receiving monitor with its batch size set to
new_size
.
# File lib/arborist/monitor/connection_batching.rb, line 234
def with_batch_size( new_size )
copy = self.clone
copy.batch_size = new_size
return copy
end
Return a clone of receiving monitor with its timeout set to
new_timeout
.
# File lib/arborist/monitor/connection_batching.rb, line 242
def with_timeout( new_timeout )
copy = self.clone
copy.timeout = new_timeout
return copy
end