class Bones::RPC::ReadPreference::Nearest

Encapsulates behaviour around a nearest read preference.

@since 0.0.1

Public Instance Methods

name() click to toggle source

Get the name for the read preference on the server side.

@example Get the name of the read preference.

nearest.name

@return [ Symbol ] :nearest.

@since 0.0.1

# File lib/bones/rpc/read_preference/nearest.rb, line 20
def name
  :nearest
end
with_node(cluster, &block) click to toggle source

Execute the provided block on the node with the lowest latency, allowing either primary or secondary.

@example Read from the nearest node in the cluster.

preference.with_node(cluster) do |node|
  node.command(ismaster: 1)
end

@note If tag sets are provided then selection will need to

match the provided tags.

@param [ Cluster ] cluster The cluster of nodes to select from. @param [ Proc ] block The block to execute on the node.

@raise [ Errors::ConnectionFailure ] If no node was available in the

cluster.

@return [ Object ] The result of the block.

@since 0.0.1

# File lib/bones/rpc/read_preference/nearest.rb, line 44
def with_node(cluster, &block)
  with_retry(cluster) do
    nearest = cluster.nodes.sort_by(&:latency).first
    if nearest
      block.call(nearest)
    else
      raise Errors::ConnectionFailure, "No nodes available to select in the cluster"
    end
  end
end