fix(stats): leave our own host out of the peers

Favicon lookups for local accounts can add our own host to instances.
Also skip userinfo and IPv6 literals in the backfill migration.
This commit is contained in:
Lain Soykaf 2026-10-27 20:53:56 +04:00
commit 958166589e
No known key found for this signature in database
5 changed files with 15 additions and 4 deletions

View file

@ -1 +1 @@
Read the peer list from the instances table and update instance stats every 5 minutes instead of every minute; the old peers query was one of the most expensive regular queries
Read the peer list from the instances table (hosts of remote actors, without our own host) and update instance stats every 5 minutes; the old peers query scanned all remote users every minute

View file

@ -67,7 +67,10 @@ defmodule Pleroma.Stats do
def calculate_stat_data do
# Hosts are unique in instances, and remote users add theirs. Scanning
# all remote users for their domains was one of the costliest queries.
peers = Repo.all(from(i in Instance, select: i.host))
# Favicon lookups for local accounts can add our own host, so skip it.
peers =
from(i in Instance, where: i.host != ^local_host(), select: i.host)
|> Repo.all()
domain_count = Enum.count(peers)
@ -93,6 +96,9 @@ defmodule Pleroma.Stats do
}
end
# Read from the config: this also runs before the endpoint has started.
defp local_host, do: Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host])
@spec get_status_visibility_count(String.t() | nil) :: map()
def get_status_visibility_count(instance \\ nil) do
if is_nil(instance) do

View file

@ -13,7 +13,8 @@ defmodule Pleroma.Repo.Migrations.BackfillInstancesFromUsers do
INSERT INTO instances (host, inserted_at, updated_at)
SELECT DISTINCT host, now(), now()
FROM (
SELECT substring(ap_id from '^[A-Za-z][A-Za-z0-9+.-]*://([^/:?#]+)') AS host
-- Skips userinfo; IPv6 literals give NULL and are left out.
SELECT substring(ap_id from '^[A-Za-z][A-Za-z0-9+.-]*://(?:[^/?#@]*@)?([^/:?#\\[]+)') AS host
FROM users
WHERE local = false AND ap_id IS NOT NULL
) AS hosts

View file

@ -19,12 +19,14 @@ defmodule Pleroma.Repo.Migrations.BackfillInstancesFromUsersTest do
insert(:user, local: false, ap_id: "https://new.example/users/a")
insert(:user, local: false, ap_id: "https://new.example/users/b")
insert(:user, local: false, ap_id: "https://ported.example:8443/users/c")
insert(:user, local: false, ap_id: "https://user@userinfo.example/users/d")
insert(:user, local: false, ap_id: "https://[2001:db8::1]/users/e")
insert(:user, local: true)
migration.up()
hosts = Instance |> Repo.all() |> Enum.map(& &1.host) |> Enum.sort()
assert hosts == ["known.example", "new.example", "ported.example"]
assert hosts == ["known.example", "new.example", "ported.example", "userinfo.example"]
assert %{unreachable_since: ^unreachable_since} = Repo.get_by(Instance, host: "known.example")
end

View file

@ -86,6 +86,8 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do
# Peers come from the instances table, which remote users add to.
insert(:instance, host: "peer1.com")
insert(:instance, host: "peer2.com")
# Favicon lookups for local accounts can add our own host.
insert(:instance, host: Pleroma.Web.Endpoint.host())
Pleroma.Stats.force_update()