blob: df7af054b79b290c1f7e6a0390d54a4a3092b090 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# 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
|