Sha256: bcff566c3217ebab83fabc07c9d8da085ee55711eec6937337f50eda3bbd2063

Contents?: true

Size: 690 Bytes

Versions: 1

Compression:

Stored size: 690 Bytes

Contents

module DistributeReads
  module GlobalMethods
    def distribute_reads(max_lag: nil)
      raise ArgumentError, "Missing block" unless block_given?

      if max_lag && DistributeReads.lag > max_lag
        raise DistributeReads::TooMuchLag, "Replica lag over #{max_lag} seconds"
      end

      previous_value = Thread.current[:distribute_reads]
      begin
        Thread.current[:distribute_reads] = true
        value = yield
        warn "[distribute_reads] Call `to_a` inside block to execute query on replica" if value.is_a?(ActiveRecord::Relation) && !previous_value
        value
      ensure
        Thread.current[:distribute_reads] = previous_value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
distribute_reads-0.1.2 lib/distribute_reads/global_methods.rb