summary refs log tree commit diff
path: root/host/Rory-nginx/services/matrix/synapse/workers/pusher.nix
diff options
context:
space:
mode:
Diffstat (limited to 'host/Rory-nginx/services/matrix/synapse/workers/pusher.nix')
-rw-r--r--host/Rory-nginx/services/matrix/synapse/workers/pusher.nix83
1 files changed, 58 insertions, 25 deletions
diff --git a/host/Rory-nginx/services/matrix/synapse/workers/pusher.nix b/host/Rory-nginx/services/matrix/synapse/workers/pusher.nix
index 64d40b2..7388824 100644
--- a/host/Rory-nginx/services/matrix/synapse/workers/pusher.nix
+++ b/host/Rory-nginx/services/matrix/synapse/workers/pusher.nix
@@ -2,7 +2,7 @@
 let
   cfg = config.services.matrix-synapse;
   dbGroup = "small";
-  pushers = lib.range 0 (cfg.pushers - 1);
+  workers = lib.range 0 (cfg.pushers - 1);
   workerName = "pusher";
   workerRoutes = {
     client = [ ];
@@ -10,6 +10,9 @@ let
     media = [ ];
   };
 in
+let
+  enabledResources = lib.optionals (lib.length workerRoutes.client > 0) [ "client" ] ++ lib.optionals (lib.length workerRoutes.federation > 0) [ "federation" ] ++ lib.optionals (lib.length workerRoutes.media > 0) [ "media" ];
+in
 {
   config = lib.mkIf (cfg.pushers > 0) {
     services.matrix-synapse = {
@@ -20,10 +23,10 @@ in
             value = {
               path = "/run/matrix-synapse/${workerName}-${toString index}.sock";
             };
-          }) pushers
+          }) workers
         );
 
-        pusher_instances = lib.map (index: "${workerName}-${toString index}") pushers;
+        pusher_instances = lib.map (index: "${workerName}-${toString index}") workers;
       };
 
       workers = lib.listToAttrs (
@@ -31,18 +34,30 @@ in
           name = "${workerName}-${toString index}";
           value = {
             worker_app = "synapse.app.generic_worker";
-            worker_listeners = [
-              {
+            worker_listeners =
+              [
+                {
+                  type = "http";
+                  path = "/run/matrix-synapse/${workerName}-${toString index}.sock";
+                  resources = [
+                    {
+                      names = [ "replication" ];
+                      compress = false;
+                    }
+                  ];
+                }
+              ]
+              ++ lib.map (type: {
                 type = "http";
-                path = "/run/matrix-synapse/${workerName}-${toString index}.sock";
+                path = "/run/matrix-synapse/${workerName}-${type}-${toString index}.sock";
+                mode = "666";
                 resources = [
                   {
-                    names = [ "replication" ];
+                    names = [ type ];
                     compress = false;
                   }
                 ];
-              }
-            ];
+              }) enabledResources;
             database = (
               import ../db.nix {
                 inherit dbGroup;
@@ -50,28 +65,46 @@ in
               }
             );
           };
-        }) pushers
+        }) workers
       );
     };
 
     services.nginx = {
-      virtualHosts."${cfg.nginxVirtualHostName}".locations = lib.listToAttrs (
-        lib.flatten (
-          lib.forEach
-            [
-              "client"
-              "federation"
-              "media"
-            ]
-            (
-              type:
-              lib.map (route: {
-                name = route;
+      upstreams = lib.listToAttrs (
+        lib.map (type: {
+          name = "${workerName}-${type}";
+          value = {
+            extraConfig = ''
+              keepalive 32;
+              least_conn;
+            '';
+            servers = lib.listToAttrs (
+              lib.map (index: {
+                name = "unix:/run/matrix-synapse/${workerName}-${type}-${toString index}.sock";
                 value = {
-                  proxyPass = "http://unix:/run/matrix-synapse/${workerName}-${type}.sock";
+                  max_fails = 0;
                 };
-              }) workerRoutes.${type}
-            )
+              }) workers
+            );
+          };
+        }) enabledResources
+      );
+
+      virtualHosts."${cfg.nginxVirtualHostName}".locations = lib.listToAttrs (
+        lib.flatten (
+          lib.forEach enabledResources (
+            type:
+            lib.map (route: {
+              name = route;
+              value = {
+                proxyPass = "http://${workerName}-${type}";
+                extraConfig = ''
+                  proxy_http_version 1.1;
+                  proxy_set_header Connection "";
+                '';
+              };
+            }) workerRoutes.${type}
+          )
         )
       );
     };