1let
2 redisPort = 6379;
3 centrifugoPort = 8080;
4 nodes = [
5 "centrifugo1"
6 "centrifugo2"
7 "centrifugo3"
8 ];
9in
10{ lib, ... }:
11{
12 name = "centrifugo";
13 meta.maintainers = [
14 lib.maintainers.tie
15 lib.maintainers.valodim
16 ];
17
18 nodes = lib.listToAttrs (
19 lib.imap0 (index: name: {
20 inherit name;
21 value =
22 { config, ... }:
23 {
24 services.centrifugo = {
25 enable = true;
26 settings = {
27 node = {
28 inherit name;
29 };
30 http_server.port = centrifugoPort;
31 http_api.insecure = true;
32 usage_stats.disabled = true;
33
34 engine.type = "redis";
35 engine.redis.address =
36 let
37 toRedisAddresses = map (name: "${name}:${toString redisPort}");
38 in
39 toRedisAddresses (lib.take index nodes)
40 ++ [
41 "unix://${config.services.redis.servers.centrifugo.unixSocket}"
42 ]
43 ++ toRedisAddresses (lib.drop (index + 1) nodes);
44 };
45 extraGroups = [
46 config.services.redis.servers.centrifugo.user
47 ];
48 };
49 services.redis.servers.centrifugo = {
50 enable = true;
51 bind = null; # all interfaces
52 port = redisPort;
53 openFirewall = true;
54 settings.protected-mode = false;
55 };
56 };
57 }) nodes
58 );
59
60 testScript = ''
61 import json
62
63 redisPort = ${toString redisPort}
64 centrifugoPort = ${toString centrifugoPort}
65
66 start_all()
67
68 for machine in machines:
69 machine.wait_for_unit("redis-centrifugo.service")
70 machine.wait_for_open_port(redisPort)
71
72 for machine in machines:
73 machine.wait_for_unit("centrifugo.service")
74 machine.wait_for_open_port(centrifugoPort)
75
76 # See https://centrifugal.dev/docs/server/server_api#info
77 def list_nodes(machine):
78 curl = "curl --fail-with-body --silent"
79 body = "{}"
80 resp = json.loads(machine.succeed(f"{curl} -d '{body}' http://localhost:{centrifugoPort}/api/info"))
81 return resp["result"]["nodes"]
82 machineNames = {m.name for m in machines}
83 for machine in machines:
84 nodes = list_nodes(machine)
85 assert len(nodes) == len(machines)
86 nodeNames = {n['name'] for n in nodes}
87 assert machineNames == nodeNames
88 '';
89}