summary refs log tree commit diff
path: root/host/Rory-nginx/services/matrix/synapse/workers/stream-writers
diff options
context:
space:
mode:
Diffstat (limited to 'host/Rory-nginx/services/matrix/synapse/workers/stream-writers')
-rw-r--r--host/Rory-nginx/services/matrix/synapse/workers/stream-writers/event-stream-writer.nix125
1 files changed, 93 insertions, 32 deletions
diff --git a/host/Rory-nginx/services/matrix/synapse/workers/stream-writers/event-stream-writer.nix b/host/Rory-nginx/services/matrix/synapse/workers/stream-writers/event-stream-writer.nix
index 1f5b638..e17fa95 100644
--- a/host/Rory-nginx/services/matrix/synapse/workers/stream-writers/event-stream-writer.nix
+++ b/host/Rory-nginx/services/matrix/synapse/workers/stream-writers/event-stream-writer.nix
@@ -1,45 +1,106 @@
 { config, lib, ... }:
+
 let
   cfg = config.services.matrix-synapse;
-  streamWriters = lib.range 0 (cfg.eventStreamWriters - 1);
-  streamWriterType = "events";
+  dbGroup = "medium";
+  streamWriterType = "event";
+  workers = lib.range 0 (cfg.eventStreamWriters - 1);
+  workerName = "event_stream_writer";
+  workerRoutes = {
+    client = [ ];
+    federation = [ ];
+    media = [ ];
+  };
 in
 {
-  services.matrix-synapse = lib.mkIf (cfg.eventStreamWriters > 0) {
-    settings = {
-      instance_map = lib.listToAttrs (
-        lib.map (port: {
-          name = "stream-writer-${streamWriterType}-${toString port}";
+  config = lib.mkIf (cfg.eventStreamWriters > 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
+        );
+
+        stream_writers.${streamWriterType} = lib.map (index: "${workerName}-${toString index}") workers;
+      };
+
+      workers = lib.listToAttrs (
+        lib.map (index: {
+          name = "${workerName}-${toString index}";
           value = {
-            path = "/run/matrix-synapse/stream_writer-${streamWriterType}-${toString port}.sock";
+            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}";
+              }
+            );
           };
-        }) streamWriters
+        }) workers
       );
+    };
 
-      stream_writers = {
-        events = lib.map (index: "stream_writer-${streamWriterType}-${toString index}") streamWriters;
-      };
+    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
+      );
     };
 
-    workers = lib.listToAttrs (
-      lib.map (index: {
-        name = "stream-writer-${toString index}";
-        value = {
-          worker_app = "synapse.app.generic_worker";
-          worker_listeners = [
-            {
-              type = "http";
-              path = "/run/matrix-synapse/stream_writer-${streamWriterType}-${toString index}.sock";
-              resources = [
-                {
-                  names = [ "replication" ];
-                  compress = false;
-                }
-              ];
-            }
-          ];
-        };
-      }) streamWriters
-    );
+    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}
+            )
+        )
+      );
+    };
   };
 }