Sha256: 80c41de77ac55a413c9163bf38c243dca66402a3732a9fa0461a1c56ab2bd898

Contents?: true

Size: 1.35 KB

Versions: 38

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module Middleware
    class DatabaseSelector
      class Resolver
        # The session class is used by the DatabaseSelector::Resolver to save
        # timestamps of the last write in the session.
        #
        # The last_write is used to determine whether it's safe to read
        # from the replica or the request needs to be sent to the primary.
        class Session # :nodoc:
          def self.call(request)
            new(request.session)
          end

          # Converts time to a timestamp that represents milliseconds since
          # epoch.
          def self.convert_time_to_timestamp(time)
            time.to_i * 1000 + time.usec / 1000
          end

          # Converts milliseconds since epoch timestamp into a time object.
          def self.convert_timestamp_to_time(timestamp)
            timestamp ? Time.at(timestamp / 1000, (timestamp % 1000) * 1000) : Time.at(0)
          end

          def initialize(session)
            @session = session
          end

          attr_reader :session

          def last_write_timestamp
            self.class.convert_timestamp_to_time(session[:last_write])
          end

          def update_last_write_timestamp
            session[:last_write] = self.class.convert_time_to_timestamp(Time.now)
          end
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 4 rubygems

Version Path
activerecord-6.0.3 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.3.rc1 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.2.2 lib/active_record/middleware/database_selector/resolver/session.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/middleware/database_selector/resolver/session.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.2.1 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.2 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.2.rc2 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.2.rc1 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.1 lib/active_record/middleware/database_selector/resolver/session.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.1.rc1 lib/active_record/middleware/database_selector/resolver/session.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.0 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.0.rc2 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.0.rc1 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.0.beta3 lib/active_record/middleware/database_selector/resolver/session.rb
activerecord-6.0.0.beta2 lib/active_record/middleware/database_selector/resolver/session.rb