module Bones::RPC::ReadPreference::Selectable

Provides the shared behaviour for read preferences that can filter by a tag set or add query options.

@since 0.0.1

Attributes

tags[R]

@!attribute tags

@return [ Array<Hash> ] The tag sets.

Public Class Methods

new(tags = nil) click to toggle source

Instantiate the new taggable read preference.

@example Instantiate the taggable.

Bones::RPC::ReadPreference::Secondary.new({ east_coast: 1 })

@param [ Array<Hash> ] tags The tag sets.

@since 0.0.1

# File lib/bones/rpc/read_preference/selectable.rb, line 24
def initialize(tags = nil)
  @tags = tags
end

Public Instance Methods

query_options(options) click to toggle source

Get the provided options as query options for this read preference.

@example Get the query options.

preference.query_options({})

@param [ Hash ] options The existing options for the query.

@return [ Hash ] The options plus additional query options.

@since 0.0.1

# File lib/bones/rpc/read_preference/selectable.rb, line 38
def query_options(options)
  options[:flags] ||= []
  options[:flags] |= [ :slave_ok ]
  options
end

Private Instance Methods

with_retry(cluster, retries = cluster.max_retries, &block) click to toggle source

Execute the provided block on the cluster and retry if the execution fails.

@api private

@example Execute with retry.

preference.with_retry(cluster) do
  cluster.with_primary do |node|
    node.refresh
  end
end

@param [ Cluster ] cluster The cluster. @param [ Integer ] retries The number of times to retry.

@return [ Object ] The result of the block.

@since 0.0.1

# File lib/bones/rpc/read_preference/selectable.rb, line 64
def with_retry(cluster, retries = cluster.max_retries, &block)
  begin
    block.call
  rescue Errors::ConnectionFailure => e
    if retries > 0
      Loggable.warn("  BONES-RPC:", "Retrying connection attempt #{retries} more time(s).", "n/a")
      sleep(cluster.retry_interval)
      cluster.refresh
      with_retry(cluster, retries - 1, &block)
    else
      raise e
    end
  end
end