Slide 61
Slide 61 text
# ActiveRecord::Middleware::DatabaseSelector::Resolver
# activerecord/lib/active_record/middleware/database_selector/resolver.rb
private
def read_from_primary(&blk)
ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role, prevent_writes: true) do
instrumenter.instrument("database_selector.active_record.read_from_primary", &blk)
end
end
def read_from_replica(&blk)
ActiveRecord::Base.connected_to(role: ActiveRecord.reading_role, prevent_writes: true) do
instrumenter.instrument("database_selector.active_record.read_from_replica", &blk)
end
end
def write_to_primary
ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role, prevent_writes: false) do
instrumenter.instrument("database_selector.active_record.wrote_to_primary") do
yield
ensure
context.update_last_write_timestamp
end
end
end