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

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/middleware/database_selector/resolver/session.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Session

Returns a new instance of Session.



28
29
30
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/middleware/database_selector/resolver/session.rb', line 28

def initialize(session)
  @session = session
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



32
33
34
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/middleware/database_selector/resolver/session.rb', line 32

def session
  @session
end

Class Method Details

.call(request) ⇒ Object

:nodoc:



13
14
15
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/middleware/database_selector/resolver/session.rb', line 13

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

.convert_time_to_timestamp(time) ⇒ Object

Converts time to a timestamp that represents milliseconds since epoch.



19
20
21
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/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) ⇒ Object

Converts milliseconds since epoch timestamp into a time object.



24
25
26
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/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 Method Details

#last_write_timestampObject



34
35
36
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/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) ⇒ Object



42
43
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/middleware/database_selector/resolver/session.rb', line 42

def save(response)
end

#update_last_write_timestampObject



38
39
40
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/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