From 63ddb2358526a7d051ff20e76d20ba1bdf1c57f5 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Mon, 16 Jun 2025 15:03:45 +0100 Subject: [PATCH] Opt out of writer affinity on readings controller --- app/controllers/application_controller.rb | 2 +- app/controllers/cards/readings_controller.rb | 2 ++ app/controllers/concerns/writer_affinity.rb | 14 ++++++++++++++ test/controllers/cards/readings_controller_test.rb | 6 ++++++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 app/controllers/concerns/writer_affinity.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 20385eda0..af52c2fd6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,5 @@ class ApplicationController < ActionController::Base - include Authentication, CurrentTimezone, SetPlatform + include Authentication, CurrentTimezone, SetPlatform, WriterAffinity stale_when_importmap_changes allow_browser versions: :modern diff --git a/app/controllers/cards/readings_controller.rb b/app/controllers/cards/readings_controller.rb index 2334cb8f5..a92c64525 100644 --- a/app/controllers/cards/readings_controller.rb +++ b/app/controllers/cards/readings_controller.rb @@ -1,6 +1,8 @@ class Cards::ReadingsController < ApplicationController include CardScoped + skip_writer_affinity + def create @notifications = @card.read_by(Current.user) end diff --git a/app/controllers/concerns/writer_affinity.rb b/app/controllers/concerns/writer_affinity.rb new file mode 100644 index 000000000..da03e1a42 --- /dev/null +++ b/app/controllers/concerns/writer_affinity.rb @@ -0,0 +1,14 @@ +module WriterAffinity + extend ActiveSupport::Concern + + class_methods do + def skip_writer_affinity(**) + before_action :set_writer_affinity_opt_out_header, ** + end + end + + private + def set_writer_affinity_opt_out_header + response.headers["X-Writer-Affinity"] = "false" + end +end diff --git a/test/controllers/cards/readings_controller_test.rb b/test/controllers/cards/readings_controller_test.rb index b6fa6d040..147dcfe4b 100644 --- a/test/controllers/cards/readings_controller_test.rb +++ b/test/controllers/cards/readings_controller_test.rb @@ -22,4 +22,10 @@ class Cards::ReadingsControllerTest < ActionDispatch::IntegrationTest assert_response :success end + + test "create opts-out of writer affinity in the load balancer" do + post card_reading_path(cards(:logo)), as: :turbo_stream + + assert_equal "false", response.headers["X-Writer-Affinity"] + end end