Skip to content

Commit 0ff6952

Browse files
BenLubarhiyuki2578
authored andcommitted
Reduce server load caused by anonymous viewing. (mastodon#9059)
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 mastodon#9035.
1 parent 9198919 commit 0ff6952

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

app/controllers/accounts_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class AccountsController < ApplicationController
1010
def show
1111
respond_to do |format|
1212
format.html do
13+
mark_cacheable! unless user_signed_in?
14+
1315
@body_classes = 'with-modals'
1416
@pinned_statuses = []
1517
@endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)
@@ -30,17 +32,21 @@ def show
3032
end
3133

3234
format.atom do
35+
mark_cacheable!
36+
3337
@entries = @account.stream_entries.where(hidden: false).with_includes.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id])
3438
render xml: OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, @entries.reject { |entry| entry.status.nil? }))
3539
end
3640

3741
format.rss do
42+
mark_cacheable!
43+
3844
@statuses = cache_collection(default_statuses.without_reblogs.without_replies.limit(PAGE_SIZE), Status)
3945
render xml: RSS::AccountSerializer.render(@account, @statuses)
4046
end
4147

4248
format.json do
43-
skip_session!
49+
mark_cacheable!
4450

4551
render_cached_json(['activitypub', 'actor', @account], content_type: 'application/activity+json') do
4652
ActiveModelSerializers::SerializableResource.new(@account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter)

app/controllers/application_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ def set_cache_headers
151151
response.headers['Vary'] = 'Accept'
152152
end
153153

154+
def mark_cacheable!
155+
skip_session!
156+
expires_in 0, public: true
157+
end
158+
154159
def skip_session!
155160
request.session_options[:skip] = true
156161
end

app/controllers/follower_accounts_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class FollowerAccountsController < ApplicationController
66
def index
77
respond_to do |format|
88
format.html do
9+
mark_cacheable! unless user_signed_in?
10+
911
next if @account.user_hides_network?
1012

1113
follows

app/controllers/statuses_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class StatusesController < ApplicationController
2727
def show
2828
respond_to do |format|
2929
format.html do
30+
mark_cacheable! unless user_signed_in?
31+
3032
@body_classes = 'with-modals'
3133

3234
set_ancestors
@@ -36,7 +38,7 @@ def show
3638
end
3739

3840
format.json do
39-
skip_session! unless @stream_entry.hidden?
41+
mark_cacheable! unless @stream_entry.hidden?
4042

4143
render_cached_json(['activitypub', 'note', @status], content_type: 'application/activity+json', public: !@stream_entry.hidden?) do
4244
ActiveModelSerializers::SerializableResource.new(@status, serializer: ActivityPub::NoteSerializer, adapter: ActivityPub::Adapter)

0 commit comments

Comments
 (0)