summary refs log tree commit diff
path: root/host/Rory-nginx/services/matrix/synapse/workers/event-creator.nix
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-08-17 10:04:28 +0200
committerRory& <root@rory.gay>2024-08-17 10:04:28 +0200
commit8fd3821f7ea8afbfb404be33df7982dbfb3b7155 (patch)
treef2cdcff78cc9b0f03883b3925f1d73fd80fac952 /host/Rory-nginx/services/matrix/synapse/workers/event-creator.nix
parentStart reworking other workers (diff)
downloadRory-Open-Architecture-8fd3821f7ea8afbfb404be33df7982dbfb3b7155.tar.xz
Stream writers, event creators, auth worker
Diffstat (limited to 'host/Rory-nginx/services/matrix/synapse/workers/event-creator.nix')
-rw-r--r--host/Rory-nginx/services/matrix/synapse/workers/event-creator.nix111
1 files changed, 111 insertions, 0 deletions
diff --git a/host/Rory-nginx/services/matrix/synapse/workers/event-creator.nix b/host/Rory-nginx/services/matrix/synapse/workers/event-creator.nix
new file mode 100644
index 0000000..c4b9c7f
--- /dev/null
+++ b/host/Rory-nginx/services/matrix/synapse/workers/event-creator.nix
@@ -0,0 +1,111 @@
+{ config, lib, ... }:
+
+let
+  cfg = config.services.matrix-synapse;
+  dbGroup = "medium";
+  workers = lib.range 0 (cfg.eventCreators - 1);
+  workerName = "event_creator";
+  workerRoutes = {
+    client =
+      [
+        "^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact"
+        "^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send"
+        "^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$"
+        "^/_matrix/client/(api/v1|r0|v3|unstable)/join/"
+        "^/_matrix/client/(api/v1|r0|v3|unstable)/knock/"
+        "^/_matrix/client/(api/v1|r0|v3|unstable)/profile/"
+      ];
+    federation = [ ];
+    media = [ ];
+  };
+in
+{
+  config = lib.mkIf (cfg.eventCreators > 0) {
+    services.matrix-synapse = {
+      settings = {
+        instance_map = lib.listToAttrs (
+          lib.map (index: {
+            name = "${workerName}-${toString index}";
+            value = {
+              path = "/run/matrix-synapse/${workerName}-${toString index}.sock";
+            };
+          }) workers
+        );
+      };
+
+      workers = lib.listToAttrs (
+        lib.map (index: {
+          name = "${workerName}-${toString index}";
+          value = {
+            worker_app = "synapse.app.generic_worker";
+            worker_listeners = [
+              {
+                type = "http";
+                path = "/run/matrix-synapse/${workerName}-${toString index}.sock";
+                resources = [
+                  {
+                    names = [ "replication" ];
+                    compress = false;
+                  }
+                ];
+              }
+              {
+                type = "http";
+                path = "/run/matrix-synapse/${workerName}-client-${toString index}.sock";
+                mode = "666";
+                resources = [
+                  {
+                    names = [ "client" ];
+                    compress = false;
+                  }
+                ];
+              }
+            ];
+            database = (
+              import ../db.nix {
+                inherit dbGroup;
+                workerName = "${workerName}-${toString index}";
+              }
+            );
+          };
+        }) workers
+      );
+    };
+
+    services.nginx.upstreams."${workerName}" = {
+      extraConfig = ''
+        keepalive 32;
+      '';
+      servers = lib.listToAttrs (
+        lib.map (index: {
+          name = "unix:/run/matrix-synapse/${workerName}-client-${toString index}.sock";
+          value = {
+            max_fails = 0;
+          };
+        }) workers
+      );
+    };
+
+    services.nginx = {
+      virtualHosts."${cfg.nginxVirtualHostName}".locations = lib.listToAttrs (
+        lib.flatten (
+          lib.forEach
+            [
+              "client"
+              "federation"
+              "media"
+            ]
+            (
+              type:
+              lib.map (route: {
+                name = route;
+                value = {
+                  proxyPass = "http://unix:/run/matrix-synapse/${workerName}-${type}.sock";
+                };
+              }) workerRoutes.${type}
+            )
+        )
+      );
+    };
+  };
+}