]> cat aescling's git repositories - mastodon.git/commitdiff
Reduce server load caused by anonymous viewing. (#9059)
authorBen Lubar <ben.lubar+github@gmail.com>
Sun, 17 Mar 2019 14:39:25 +0000 (09:39 -0500)
committerEugen Rochko <eugen@zeonfederated.com>
Sun, 17 Mar 2019 14:39:25 +0000 (15:39 +0100)
Do not start a session if the current user is not logged in for public-facing pages.

Mark pages that don't care about sessions as publicly cacheable.

Keep the max age as 0 so proxies and browsers will still try to retrieve an updated version but can still fall back to the stale version if the site is down or too slow.

Fixes #9035.

app/controllers/accounts_controller.rb
app/controllers/application_controller.rb
app/controllers/follower_accounts_controller.rb
app/controllers/statuses_controller.rb

index a3410c1efec3cb712aeb4ab76ffa499facc7bbdd..abc68d2a464f20f7dd39fef9db31d8a9bcb6ded5 100644 (file)
@@ -10,6 +10,8 @@ class AccountsController < ApplicationController
   def show
     respond_to do |format|
       format.html do
+        mark_cacheable! unless user_signed_in?
+
         @body_classes      = 'with-modals'
         @pinned_statuses   = []
         @endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)
@@ -30,17 +32,21 @@ class AccountsController < ApplicationController
       end
 
       format.atom do
+        mark_cacheable!
+
         @entries = @account.stream_entries.where(hidden: false).with_includes.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id])
         render xml: OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, @entries.reject { |entry| entry.status.nil? }))
       end
 
       format.rss do
+        mark_cacheable!
+
         @statuses = cache_collection(default_statuses.without_reblogs.without_replies.limit(PAGE_SIZE), Status)
         render xml: RSS::AccountSerializer.render(@account, @statuses)
       end
 
       format.json do
-        skip_session!
+        mark_cacheable!
 
         render_cached_json(['activitypub', 'actor', @account], content_type: 'application/activity+json') do
           ActiveModelSerializers::SerializableResource.new(@account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter)
index b54e7b008eeb06283bb7b7a94e39ddb6a4dbaf97..990aff857a05f893c1ace23900c98dfa2a5e03f2 100644 (file)
@@ -151,6 +151,11 @@ class ApplicationController < ActionController::Base
     response.headers['Vary'] = 'Accept'
   end
 
+  def mark_cacheable!
+    skip_session!
+    expires_in 0, public: true
+  end
+
   def skip_session!
     request.session_options[:skip] = true
   end
index 99cb3676f1e30dec6bef4508f430e3315d742d96..aea42bd632ea8b346c294dbae577294e791d9556 100644 (file)
@@ -6,6 +6,8 @@ class FollowerAccountsController < ApplicationController
   def index
     respond_to do |format|
       format.html do
+        mark_cacheable! unless user_signed_in?
+
         next if @account.user_hides_network?
 
         follows
index 3686bd9fd827eca8d02ccf61dc801c092d06b238..fc44d5fb1d09cdc37441f906ec097cadb35cf602 100644 (file)
@@ -27,6 +27,8 @@ class StatusesController < ApplicationController
   def show
     respond_to do |format|
       format.html do
+        mark_cacheable! unless user_signed_in?
+
         @body_classes = 'with-modals'
 
         set_ancestors
@@ -36,7 +38,7 @@ class StatusesController < ApplicationController
       end
 
       format.json do
-        skip_session! unless @stream_entry.hidden?
+        mark_cacheable! unless @stream_entry.hidden?
 
         render_cached_json(['activitypub', 'note', @status], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
           ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter)