diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb index 0d02294eb..94dba1d03 100644 --- a/app/controllers/api/v1/accounts_controller.rb +++ b/app/controllers/api/v1/accounts_controller.rb @@ -52,7 +52,7 @@ class Api::V1::AccountsController < ApiController set_maps(@statuses) set_counters_maps(@statuses) - next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT) + next_path = statuses_api_v1_account_url(max_id: @statuses.last.id) unless @statuses.empty? prev_path = statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty? set_pagination_headers(next_path, prev_path) @@ -66,7 +66,7 @@ class Api::V1::AccountsController < ApiController set_maps(@statuses) set_counters_maps(@statuses) - next_path = media_statuses_api_v1_account_url(max_id: @statuses.last.id) if @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT) + next_path = media_statuses_api_v1_account_url(max_id: @statuses.last.id) unless @statuses.empty? prev_path = media_statuses_api_v1_account_url(since_id: @statuses.first.id) unless @statuses.empty? set_pagination_headers(next_path, prev_path) diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb index 877356a75..544ba2442 100644 --- a/app/controllers/api/v1/notifications_controller.rb +++ b/app/controllers/api/v1/notifications_controller.rb @@ -17,7 +17,7 @@ class Api::V1::NotificationsController < ApiController set_counters_maps(statuses) set_account_counters_maps(@notifications.map(&:from_account)) - next_path = api_v1_notifications_url(max_id: @notifications.last.id) if @notifications.size == limit_param(DEFAULT_NOTIFICATIONS_LIMIT) + next_path = api_v1_notifications_url(max_id: @notifications.last.id) unless @notifications.empty? prev_path = api_v1_notifications_url(since_id: @notifications.first.id) unless @notifications.empty? set_pagination_headers(next_path, prev_path) diff --git a/app/controllers/api/v1/timelines_controller.rb b/app/controllers/api/v1/timelines_controller.rb index a8cc2b288..af6e5b7df 100644 --- a/app/controllers/api/v1/timelines_controller.rb +++ b/app/controllers/api/v1/timelines_controller.rb @@ -14,7 +14,7 @@ class Api::V1::TimelinesController < ApiController set_counters_maps(@statuses) set_account_counters_maps(@statuses.flat_map { |s| [s.account, s.reblog? ? s.reblog.account : nil] }.compact.uniq) - next_path = api_v1_home_timeline_url(max_id: @statuses.last.id) if @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT) + next_path = api_v1_home_timeline_url(max_id: @statuses.last.id) unless @statuses.empty? prev_path = api_v1_home_timeline_url(since_id: @statuses.first.id) unless @statuses.empty? set_pagination_headers(next_path, prev_path) @@ -30,7 +30,7 @@ class Api::V1::TimelinesController < ApiController set_counters_maps(@statuses) set_account_counters_maps(@statuses.flat_map { |s| [s.account, s.reblog? ? s.reblog.account : nil] }.compact.uniq) - next_path = api_v1_public_timeline_url(max_id: @statuses.last.id) if @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT) + next_path = api_v1_public_timeline_url(max_id: @statuses.last.id) unless @statuses.empty? prev_path = api_v1_public_timeline_url(since_id: @statuses.first.id) unless @statuses.empty? set_pagination_headers(next_path, prev_path) @@ -47,7 +47,7 @@ class Api::V1::TimelinesController < ApiController set_counters_maps(@statuses) set_account_counters_maps(@statuses.flat_map { |s| [s.account, s.reblog? ? s.reblog.account : nil] }.compact.uniq) - next_path = api_v1_hashtag_timeline_url(params[:id], max_id: @statuses.last.id) if @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT) + next_path = api_v1_hashtag_timeline_url(params[:id], max_id: @statuses.last.id) unless @statuses.empty? prev_path = api_v1_hashtag_timeline_url(params[:id], since_id: @statuses.first.id) unless @statuses.empty? set_pagination_headers(next_path, prev_path) diff --git a/app/services/favourite_service.rb b/app/services/favourite_service.rb index 824729ed6..818898302 100644 --- a/app/services/favourite_service.rb +++ b/app/services/favourite_service.rb @@ -22,10 +22,13 @@ class FavouriteService < BaseService private def build_xml(favourite) + description = "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}" + Nokogiri::XML::Builder.new do |xml| entry(xml, true) do unique_id xml, favourite.created_at, favourite.id, 'Favourite' - title xml, "#{favourite.account.acct} favourited a status by #{favourite.status.account.acct}" + title xml, description + content xml, description author(xml) do include_author xml, favourite.account diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb index d67b1bf2d..915f95b4c 100644 --- a/app/services/follow_service.rb +++ b/app/services/follow_service.rb @@ -55,10 +55,13 @@ class FollowService < BaseService end def build_follow_request_xml(follow_request) + description = "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}" + Nokogiri::XML::Builder.new do |xml| entry(xml, true) do unique_id xml, follow_request.created_at, follow_request.id, 'FollowRequest' - title xml, "#{follow_request.account.acct} requested to follow #{follow_request.target_account.acct}" + title xml, description + content xml, description author(xml) do include_author xml, follow_request.account @@ -75,10 +78,13 @@ class FollowService < BaseService end def build_follow_xml(follow) + description = "#{follow.account.acct} started following #{follow.target_account.acct}" + Nokogiri::XML::Builder.new do |xml| entry(xml, true) do unique_id xml, follow.created_at, follow.id, 'Follow' - title xml, "#{follow.account.acct} started following #{follow.target_account.acct}" + title xml, description + content xml, description author(xml) do include_author xml, follow.account diff --git a/app/services/unfavourite_service.rb b/app/services/unfavourite_service.rb index 1d3e6f06d..5f0ba4254 100644 --- a/app/services/unfavourite_service.rb +++ b/app/services/unfavourite_service.rb @@ -13,10 +13,13 @@ class UnfavouriteService < BaseService private def build_xml(favourite) + description = "#{favourite.account.acct} no longer favourites a status by #{favourite.status.account.acct}" + Nokogiri::XML::Builder.new do |xml| entry(xml, true) do unique_id xml, Time.now.utc, favourite.id, 'Favourite' - title xml, "#{favourite.account.acct} no longer favourites a status by #{favourite.status.account.acct}" + title xml, description + content xml, description author(xml) do include_author xml, favourite.account diff --git a/app/services/unfollow_service.rb b/app/services/unfollow_service.rb index 07f9b93dd..3440da364 100644 --- a/app/services/unfollow_service.rb +++ b/app/services/unfollow_service.rb @@ -13,10 +13,13 @@ class UnfollowService < BaseService private def build_xml(follow) + description = "#{follow.account.acct} is no longer following #{follow.target_account.acct}" + Nokogiri::XML::Builder.new do |xml| entry(xml, true) do unique_id xml, Time.now.utc, follow.id, 'Follow' - title xml, "#{follow.account.acct} is no longer following #{follow.target_account.acct}" + title xml, description + content xml, description author(xml) do include_author xml, follow.account diff --git a/docs/README.md b/docs/README.md index 5036ea22c..d35dece14 100644 --- a/docs/README.md +++ b/docs/README.md @@ -22,6 +22,7 @@ Index - [Development guide](Running-Mastodon/Development-guide.md) - [Alternative: Development with Vagrant](Running-Mastodon/Vagrant-guide.md) - [Administration guide](Running-Mastodon/Administration-guide.md) +- [Tuning Mastodon](Running-Mastodon/Tuning.md) ### Contributing to Mastodon - [Sponsors](Contributing-to-Mastodon/Sponsors.md) diff --git a/docs/Running-Mastodon/Tuning.md b/docs/Running-Mastodon/Tuning.md new file mode 100644 index 000000000..c4acb9927 --- /dev/null +++ b/docs/Running-Mastodon/Tuning.md @@ -0,0 +1,104 @@ +Tuning Mastodon +=============== + +Mastodon has three types of processes: + +- web +- streaming API +- background processing + +By default, the web type spawns two worker processes with 5 threads each, the streaming API is a single thread/process with 10 database pool connections, and background processing spawns one process with 5 threads. + +### Web + +The web process serves short-lived HTTP requests for most of the application. The following environment variables control it: + +- `WEB_CONCURRENCY` controls the number of worker processes +- `MAX_THREADS` controls the number of threads per process + +The default is 2 workers with 5 threads each. Threads share the memory of their parent process. Different processes allocate their own memory each. Threads in Ruby are not native threads, so it's more or less: threads equal concurrency, processes equal parallelism. A larger number of threads maxes out your CPU first, a larger number of processes maxes out your RAM first. + +These values affect how many HTTP requests can be served at the same time. When not enough threads are available, requests are queued until they can be answered. + +For a single-user instance, 1 process with 5 threads should be more than enough. + +### Streaming API + +The streaming API handles long-lived HTTP and WebSockets connections, through which clients receive real-time updates. It is a single-threaded process. By default it has a database connection pool of 10, which means 10 different database queries can run *at the same time*. The database is not heavily used in the streaming API, only for initial authentication of the request, and for some special receiver-specific filter queries when receiving new messages. At the time of writing this value cannot be reconfigured, but mostly doesn't need to. + +If you need to scale up the streaming API, spawn more separate processes on different ports (e.g. 4000, 4001, 4003, etc) and load-balance between them with nginx. + +### Background processing + +Many tasks in Mastodon are delegated to background processing to ensure the HTTP requests are fast, and to prevent HTTP request aborts from affecting the execution of those tasks. Sidekiq is a single process, with a configurable numbero of threads. By default, it is 5. That means, 5 different jobs can be executed at the same time. Others will be queued until they can be processed. + +While the amount of threads in the web process affects the responsiveness of the Mastodon instance to the end-user, the amount of threads allocated to background processing affects how quickly posts can be delivered from the author to anyone else, how soon e-mails are sent out, etc. + +The amount of threads is not controlled by an environment variable in this case, but a command line argument in the invocation of Sidekiq: + + bundle exec sidekiq -c 15 -q default -q mailers -q push + +Would start the sidekiq process with 15 threads. Please mind that each threads needs to be able to connect to the database, which means that the database pool needs to be large enough to support all the threads. The database pool size is controlled with the `DB_POOL` environment variable, and defaults to the value of `MAX_THREADS` (therefore, is 5 by default). + +You might notice that the above command specifies three queues to be processed: + +- "default" contains most tasks such as delivering messages to followers and processing incoming notifications from other instances +- "mailers" contains tasks that send e-mails +- "push" contains tasks that deliver messages to other instances + +If you wish, you could start three different processes for each queue, to ensure that even when there is a lot of tasks of one type, important tasks of other types still get executed in a timely manner. + +___ + +### How to set environment variables +#### With systemd + +In the `.service` file: + +```systemd +... +Environment="WEB_CONCURRENCY=1" +Environment="MAX_THREADS=5" +ExecStart="..." +... +``` + +Don't forget to `sudo systemctl daemon-reload` before restarting the services so that the changes would take effect! + +#### With docker-compose + +Edit `docker-compose.yml`: + +```yml +... + web: + restart: always + build: . + env_file: .env.production + environment: + - WEB_CONCURRENCY=1 + - MAX_THREADS=5 +... +``` + +Re-create the containers with `docker-compose up -d` for the changes to take effect. + +You can also scale the number of containers per "service" (where service is "web", "sidekiq" and "streaming"): + + docker-compose scale web=1 sidekiq=2 streaming=3 + +Realistically the `docker-compose.yml` file needs to be modified a bit further for the above to work, because by default it wants to bind the web container to host port 3000 and streaming container to host port 4000, of either of which there is only one on the host system. However, if you change: + +```yml +ports: + - "3000:3000" +``` + +to simply: + +```yml +ports: + - "3000" +``` + +for each service respectively, Docker will allocate random host ports of the services, allowing multiple containers to run alongside each other. But it will be on you to look up which host ports those are (e.g. with `docker ps`), and they will be different on each container restart.