123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Middleware::DatabaseSelector::Resolver::Session

Do not use. This class is for internal use only.

Overview

The session class is used by the ::ActiveRecord::Middleware::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 Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(session) ⇒ Session

[ GitHub ]

  
# File 'activerecord/lib/active_record/middleware/database_selector/resolver/session.rb', line 28

def initialize(session)
  @session = session
end

Class Method Details

.call(request)

[ GitHub ]

  
# File 'activerecord/lib/active_record/middleware/database_selector/resolver/session.rb', line 13

def self.call(request)
  new(request.session)
end

.convert_time_to_timestamp(time)

Converts time to a timestamp that represents milliseconds since epoch.

[ GitHub ]

  
# File 'activerecord/lib/active_record/middleware/database_selector/resolver/session.rb', line 19

def self.convert_time_to_timestamp(time)
  time.to_i * 1000 + time.usec / 1000
end

.convert_timestamp_to_time(timestamp)

Converts milliseconds since epoch timestamp into a time object.

[ GitHub ]

  
# File 'activerecord/lib/active_record/middleware/database_selector/resolver/session.rb', line 24

def self.convert_timestamp_to_time(timestamp)
  timestamp ? Time.at(timestamp / 1000, (timestamp % 1000) * 1000) : Time.at(0)
end

Instance Attribute Details

#session (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/middleware/database_selector/resolver/session.rb', line 32

attr_reader :session

Instance Method Details

#last_write_timestamp

[ GitHub ]

  
# File 'activerecord/lib/active_record/middleware/database_selector/resolver/session.rb', line 34

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

#save(response)

[ GitHub ]

  
# File 'activerecord/lib/active_record/middleware/database_selector/resolver/session.rb', line 42

def save(response)
end

#update_last_write_timestamp

[ GitHub ]

  
# File 'activerecord/lib/active_record/middleware/database_selector/resolver/session.rb', line 38

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