Sha256: 94c7f10447675ab873d2fef0a5479a20080e04937f13e86b6773b2ae08d1631f

Contents?: true

Size: 1.49 KB

Versions: 17

Compression:

Stored size: 1.49 KB

Contents

# encoding: utf-8
module Moped
  module ReadPreference

    # Encapsulates behaviour around a nearest read preference.
    #
    # @since 2.0.0
    class Nearest
      include Selectable

      # 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 2.0.0
      def name
        :nearest
      end

      # 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 2.0.0
      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
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/bundler/gems/moped-cf817ca58a85/lib/moped/read_preference/nearest.rb
moped-2.0.7 lib/moped/read_preference/nearest.rb
moped-2.0.6 lib/moped/read_preference/nearest.rb
moped-2.0.5 lib/moped/read_preference/nearest.rb
moped-2.0.4 lib/moped/read_preference/nearest.rb
moped-2.0.3 lib/moped/read_preference/nearest.rb
moped-2.0.2 lib/moped/read_preference/nearest.rb
moped-2.0.1 lib/moped/read_preference/nearest.rb
moped-2.0.0 lib/moped/read_preference/nearest.rb
moped-2.0.0.rc2 lib/moped/read_preference/nearest.rb
moped-2.0.0.rc1 lib/moped/read_preference/nearest.rb
moped-2.0.0.beta6 lib/moped/read_preference/nearest.rb
moped-2.0.0.beta5 lib/moped/read_preference/nearest.rb
moped-2.0.0.beta4 lib/moped/read_preference/nearest.rb
moped-2.0.0.beta3 lib/moped/read_preference/nearest.rb
moped-2.0.0.beta2 lib/moped/read_preference/nearest.rb
moped-2.0.0.beta lib/moped/read_preference/nearest.rb