From 5e2dafaa19109ac3ce9a941408c85acf85e902ed Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 4 Jul 2024 19:09:51 +0200 Subject: More workers? --- .../services/matrix/synapse/synapse-main.nix | 5 +- .../services/matrix/synapse/workers/media-repo.nix | 100 +++++++++++++++++++++ .../services/matrix/synapse/workers/module.nix | 13 +++ .../services/matrix/synapse/workers/sync.nix | 98 ++++++++++++++++++++ 4 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 host/Rory-nginx/services/matrix/synapse/workers/media-repo.nix create mode 100644 host/Rory-nginx/services/matrix/synapse/workers/sync.nix diff --git a/host/Rory-nginx/services/matrix/synapse/synapse-main.nix b/host/Rory-nginx/services/matrix/synapse/synapse-main.nix index 82e9eef..66292a3 100755 --- a/host/Rory-nginx/services/matrix/synapse/synapse-main.nix +++ b/host/Rory-nginx/services/matrix/synapse/synapse-main.nix @@ -17,10 +17,13 @@ nginxVirtualHostName = "matrix.rory.gay"; federationSenders = 32; pushers = 2; - enableStreamWriters = false; + mediaRepoWorkers = 8; + enableAppserviceWorker = true; enableBackgroundWorker = true; enableUserDirWorker = true; + + enableStreamWriters = false; #eventStreamWriters = 8; # https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html diff --git a/host/Rory-nginx/services/matrix/synapse/workers/media-repo.nix b/host/Rory-nginx/services/matrix/synapse/workers/media-repo.nix new file mode 100644 index 0000000..ec07eb0 --- /dev/null +++ b/host/Rory-nginx/services/matrix/synapse/workers/media-repo.nix @@ -0,0 +1,100 @@ +{ + config, + pkgs, + lib, + ... +}: + +let + cfg = config.services.matrix-synapse; + workers = lib.range 0 (cfg.mediaRepoWorkers - 1); + routes = [ + "~ ^/_matrix/media/" + "~ ^/_synapse/admin/v1/purge_media_cache$" + "~ ^/_synapse/admin/v1/room/.*/media.*$" + "~ ^/_synapse/admin/v1/user/.*/media.*$" + "~ ^/_synapse/admin/v1/media/.*$" + "~ ^/_synapse/admin/v1/quarantine_media/.*$" + ]; +in +{ + config = lib.mkIf (cfg.mediaRepoWorkers > 0) { + services.matrix-synapse = { + settings = { + instance_map = lib.listToAttrs ( + lib.map (index: { + name = "media_repo-${toString index}"; + value = { + path = "/run/matrix-synapse/media_repo-${toString index}.sock"; + }; + }) workers + ); + + media_instance_running_background_jobs = "media_repo-0"; + enable_media_repo = false; + }; + + workers = lib.listToAttrs ( + lib.map (index: { + name = "media_repo-${toString index}"; + value = { + worker_app = "synapse.app.generic_worker"; + worker_listeners = [ + { + type = "http"; + path = "/run/matrix-synapse/media_repo-${toString index}.sock"; + resources = [ + { + names = [ "replication" ]; + compress = false; + } + ]; + } + { + type = "http"; + path = "/run/matrix-synapse/media_repo-media-${toString index}.sock"; + mode = "666"; + resources = [ + { + names = [ "media" ]; + compress = false; + } + ]; + } + ]; + database = ( + import ../db.nix { + workerName = "media_repo-${toString index}"; + dbGroup = "small"; + } + ); + enable_media_repo = true; + }; + }) workers + ); + }; + + services.nginx.upstreams."media_repo" = { + extraConfig = '' + keepalive 32; + ''; + servers = lib.listToAttrs ( + lib.map (index: { + name = "unix:/run/matrix-synapse/media_repo-media-${toString index}.sock"; + value = { + max_fails = 0; + }; + }) workers + ); + }; + + services.nginx.virtualHosts."${cfg.nginxVirtualHostName}".locations = lib.listToAttrs ( + lib.map (route: { + name = route; + value = { + proxyPass = "http://media_repo"; + }; + }) routes + ); + }; +} diff --git a/host/Rory-nginx/services/matrix/synapse/workers/module.nix b/host/Rory-nginx/services/matrix/synapse/workers/module.nix index 84b8a87..080f524 100644 --- a/host/Rory-nginx/services/matrix/synapse/workers/module.nix +++ b/host/Rory-nginx/services/matrix/synapse/workers/module.nix @@ -19,8 +19,11 @@ in ./appservice.nix ./background.nix ./federation-sender.nix + ./media-repo.nix ./pusher.nix + ./sync.nix ./user-dir.nix + ./stream-writers/event-stream-writer.nix ]; options.services.matrix-synapse = @@ -60,6 +63,16 @@ in default = 0; description = "Number of pushers"; }; + mediaRepoWorkers = lib.mkOption { + type = lib.types.int; + default = 0; + description = "Number of media repo workers"; + }; + syncWorkers = lib.mkOption { + type = lib.types.int; + default = 0; + description = "Number of sync workers"; + }; nginxVirtualHostName = lib.mkOption { type = lib.types.str; diff --git a/host/Rory-nginx/services/matrix/synapse/workers/sync.nix b/host/Rory-nginx/services/matrix/synapse/workers/sync.nix new file mode 100644 index 0000000..d409d54 --- /dev/null +++ b/host/Rory-nginx/services/matrix/synapse/workers/sync.nix @@ -0,0 +1,98 @@ +{ + config, + pkgs, + lib, + ... +}: + +let + cfg = config.services.matrix-synapse; + workers = lib.range 0 (cfg.syncWorkers - 1); + routes = [ + "~ ^/_matrix/client/(v2_alpha|r0|v3)/sync$" + "~ ^/_matrix/client/(api/v1|v2_alpha|r0|v3)/events$" + "~ ^/_matrix/client/(api/v1|r0|v3)/initialSync$" + "~ ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$" + ]; +in +{ + config = lib.mkIf (cfg.syncWorkers > 0) { + services.matrix-synapse = { + settings = { + instance_map = lib.listToAttrs ( + lib.map (index: { + name = "sync-${toString index}"; + value = { + path = "/run/matrix-synapse/sync-${toString index}.sock"; + }; + }) workers + ); + + media_instance_running_background_jobs = "sync-0"; + enable_sync = false; + }; + + workers = lib.listToAttrs ( + lib.map (index: { + name = "sync-${toString index}"; + value = { + worker_app = "synapse.app.generic_worker"; + worker_listeners = [ + { + type = "http"; + path = "/run/matrix-synapse/sync-${toString index}.sock"; + resources = [ + { + names = [ "replication" ]; + compress = false; + } + ]; + } + { + type = "http"; + path = "/run/matrix-synapse/sync-client-${toString index}.sock"; + mode = "666"; + resources = [ + { + names = [ "client" ]; + compress = false; + } + ]; + } + ]; + database = ( + import ../db.nix { + workerName = "sync-${toString index}"; + dbGroup = "small"; + } + ); + enable_sync = true; + }; + }) workers + ); + }; + + services.nginx.upstreams."sync" = { + extraConfig = '' + keepalive 32; + ''; + servers = lib.listToAttrs ( + lib.map (index: { + name = "unix:/run/matrix-synapse/sync-client-${toString index}.sock"; + value = { + max_fails = 0; + }; + }) workers + ); + }; + + services.nginx.virtualHosts."${cfg.nginxVirtualHostName}".locations = lib.listToAttrs ( + lib.map (route: { + name = route; + value = { + proxyPass = "http://sync"; + }; + }) routes + ); + }; +} -- cgit 1.4.1