djshard 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +102 -21
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -1,58 +1,139 @@
|
|
|
1
1
|
# djshard
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Clustered and cross-host sharding for Discord bots on [Bun](https://bun.sh), plus a Kubernetes operator.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
djshard combines what [discord-hybrid-sharding](https://github.com/meister03/discord-hybrid-sharding) and [discord-cross-hosting](https://github.com/meister03/discord-cross-hosting) do:
|
|
6
|
+
|
|
7
|
+
- **Clusters**: one process runs several shards, so you need far fewer processes than shards.
|
|
8
|
+
- **Cross-host**: shards are split across machines, and `broadcastEval` reaches every cluster on every machine.
|
|
9
|
+
- **Kubernetes**: a Go operator turns a `ShardCluster` resource into a StatefulSet and acts as the bridge between pods.
|
|
10
|
+
|
|
11
|
+
djshard is not tied to any Discord library. It tells each cluster which shards to run, and you pass them to your client.
|
|
12
|
+
|
|
13
|
+
> [!NOTE]
|
|
14
|
+
> djshard runs on Bun only. It uses `Bun.spawn` and `Bun.serve`.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
bun add djshard
|
|
20
|
+
# or
|
|
21
|
+
npm install djshard
|
|
22
|
+
pnpm add djshard
|
|
23
|
+
yarn add djshard
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Both ESM (`import`) and CommonJS (`require`) builds are published. Any package manager can install it,
|
|
27
|
+
but the code must run on Bun: it uses `Bun.spawn` and `Bun.serve`.
|
|
28
|
+
|
|
29
|
+
## Single machine
|
|
6
30
|
|
|
7
31
|
```ts
|
|
8
32
|
// manager.ts
|
|
9
33
|
import { ClusterManager } from "djshard";
|
|
10
|
-
|
|
34
|
+
|
|
35
|
+
const manager = new ClusterManager("./bot.ts", { shardsPerCluster: 4 });
|
|
36
|
+
manager.on("exit", (id, code) => console.warn(`cluster ${id} exited with ${code}`));
|
|
11
37
|
await manager.spawn();
|
|
12
38
|
```
|
|
13
39
|
|
|
14
40
|
```ts
|
|
15
|
-
// bot.ts
|
|
41
|
+
// bot.ts: runs once per cluster
|
|
16
42
|
import { Client } from "discord.js";
|
|
17
43
|
import { ClusterClient, getInfo } from "djshard";
|
|
44
|
+
|
|
18
45
|
const info = getInfo();
|
|
19
46
|
const client = new Client({ intents: [], shards: info.shards, shardCount: info.totalShards });
|
|
20
47
|
client.cluster = new ClusterClient(client);
|
|
21
48
|
await client.login();
|
|
22
49
|
|
|
23
|
-
|
|
50
|
+
// Runs on every cluster; results are ordered by cluster id.
|
|
51
|
+
const counts = await client.cluster.broadcastEval((c) => c.guilds.cache.size);
|
|
52
|
+
const same = await client.cluster.fetchClientValues("guilds.cache.size");
|
|
24
53
|
```
|
|
25
54
|
|
|
26
|
-
|
|
55
|
+
When `totalShards` is `"auto"` (the default), the manager asks Discord for the recommended shard count using `DISCORD_TOKEN`.
|
|
56
|
+
|
|
57
|
+
### `ClusterManager` options
|
|
58
|
+
|
|
59
|
+
| Option | Default | Description |
|
|
60
|
+
| ------------------ | -------------------- | --------------------------------------------------------------------- |
|
|
61
|
+
| `totalShards` | `"auto"` | Total shard count. Ignored when a bridge is used, because it decides. |
|
|
62
|
+
| `shardsPerCluster` | `4` | Shards per process. |
|
|
63
|
+
| `token` | `DISCORD_TOKEN` | Used to look up the recommended shard count. |
|
|
64
|
+
| `spawnDelay` | `5500` | Milliseconds between cluster spawns, to respect identify rate limits. |
|
|
65
|
+
| `respawn` | `true` | Restart clusters that exit. |
|
|
66
|
+
| `bridge` | from `DJSHARD_*` env | `{ url, auth, id? }`. Turns on cross-host mode. |
|
|
67
|
+
|
|
68
|
+
`broadcastEval(script, { context, cluster, timeout })` passes `context` to the script as its second argument and runs it only on `cluster` when one is given. Scripts are serialized with `toString()`, so they must not capture outer variables. Pass values through `context` instead. Return values must be JSON- or structured-clone-serializable.
|
|
69
|
+
|
|
70
|
+
## Multiple machines (without Kubernetes)
|
|
71
|
+
|
|
72
|
+
Run one bridge:
|
|
27
73
|
|
|
28
74
|
```ts
|
|
29
75
|
import { Bridge } from "djshard";
|
|
30
|
-
|
|
76
|
+
|
|
77
|
+
const bridge = new Bridge({ port: 4444, auth: process.env.BRIDGE_AUTH!, machines: 3, totalShards: "auto", token });
|
|
78
|
+
await bridge.listen();
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
On every machine:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const manager = new ClusterManager("./bot.ts", {
|
|
85
|
+
bridge: { url: "ws://bridge-host:4444", auth: process.env.BRIDGE_AUTH!, id: "machine-a" },
|
|
86
|
+
});
|
|
87
|
+
await manager.spawn();
|
|
31
88
|
```
|
|
32
89
|
|
|
33
|
-
|
|
90
|
+
The bridge gives each machine a contiguous range of clusters. A machine that reconnects with the same `id` gets the same range back. Its clusters keep running while it is disconnected.
|
|
34
91
|
|
|
35
92
|
## Kubernetes
|
|
36
93
|
|
|
37
94
|
```sh
|
|
38
95
|
kubectl apply -f operator/config/crd.yaml -f operator/config/operator.yaml
|
|
39
|
-
kubectl create secret generic discord --from-literal=token
|
|
96
|
+
kubectl create secret generic discord --from-literal=token=YOUR_TOKEN
|
|
40
97
|
kubectl apply -f operator/config/example.yaml
|
|
41
98
|
```
|
|
42
99
|
|
|
43
|
-
|
|
100
|
+
```yaml
|
|
101
|
+
apiVersion: djshard.ro80t.com/v1alpha1
|
|
102
|
+
kind: ShardCluster
|
|
103
|
+
metadata: { name: mybot }
|
|
104
|
+
spec:
|
|
105
|
+
replicas: 3
|
|
106
|
+
shardsPerCluster: 4
|
|
107
|
+
# totalShards: 32 # omit to use Discord's recommendation (resolved once)
|
|
108
|
+
tokenSecretRef: { name: discord, key: token }
|
|
109
|
+
template:
|
|
110
|
+
spec:
|
|
111
|
+
containers:
|
|
112
|
+
- name: bot
|
|
113
|
+
image: mybot:latest
|
|
114
|
+
command: [bun, manager.ts]
|
|
115
|
+
```
|
|
44
116
|
|
|
45
|
-
|
|
117
|
+
For each `ShardCluster`, the operator:
|
|
46
118
|
|
|
47
|
-
-
|
|
48
|
-
- `
|
|
49
|
-
-
|
|
50
|
-
-
|
|
119
|
+
- creates a StatefulSet and a Secret holding a random bridge token,
|
|
120
|
+
- injects `DJSHARD_BRIDGE_URL`, `DJSHARD_AUTH`, `DJSHARD_ID` and `DISCORD_TOKEN` into the first container,
|
|
121
|
+
- serves the bridge itself and assigns shards by pod ordinal (`mybot-2` gets slot 2),
|
|
122
|
+
- rolls every pod when `replicas`, `totalShards` or `shardsPerCluster` changes.
|
|
51
123
|
|
|
52
|
-
|
|
124
|
+
Your `manager.ts` is the same as in single-machine mode, because `ClusterManager` picks up the `DJSHARD_*` variables automatically.
|
|
53
125
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
126
|
+
The operator image is published to `ghcr.io/ro80t/djshard-operator` with every release, tagged with the release version and `latest`. To run your own build, build `operator/Dockerfile` and change the image in `operator/config/operator.yaml`.
|
|
127
|
+
|
|
128
|
+
## Limitations
|
|
129
|
+
|
|
130
|
+
- Run the operator as a single replica. Bot connections live in its memory.
|
|
131
|
+
- During a rolling restart, an old pod and a new pod can briefly own the same shard. Discord drops the older session.
|
|
132
|
+
- Bridge traffic is plain WebSocket protected only by the shared token. Keep it inside the cluster or private network.
|
|
133
|
+
- A cluster that hangs without exiting is not detected (there is no heartbeat).
|
|
134
|
+
- Identify rate limits (`max_concurrency`) are only respected within one machine, through `spawnDelay`.
|
|
135
|
+
- Only the process mode exists. There is no worker-thread mode or custom IPC messages yet.
|
|
136
|
+
|
|
137
|
+
## Contributing
|
|
138
|
+
|
|
139
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "djshard",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Clustered + cross-host sharding for Discord bots on Bun, with a Kubernetes operator",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/ro80t/djshard.git"
|
|
8
|
+
},
|
|
5
9
|
"files": [
|
|
6
10
|
"dist"
|
|
7
11
|
],
|
|
@@ -29,12 +33,16 @@
|
|
|
29
33
|
"typecheck": "bun check",
|
|
30
34
|
"test": "bun test",
|
|
31
35
|
"ci": "bun run lint && bun run fmt:check && bun run typecheck && bun test && bun run build",
|
|
32
|
-
"prepublishOnly": "bun run build"
|
|
36
|
+
"prepublishOnly": "bun run build",
|
|
37
|
+
"skills": "skills experimental_install",
|
|
38
|
+
"release": "bun run build && changeset publish"
|
|
33
39
|
},
|
|
34
40
|
"devDependencies": {
|
|
41
|
+
"@changesets/cli": "^3.0.3",
|
|
35
42
|
"@types/bun": "latest",
|
|
36
43
|
"oxfmt": "^0.78.0",
|
|
37
44
|
"oxlint": "^1",
|
|
45
|
+
"skills": "^1.8.1",
|
|
38
46
|
"tsdown": "^0.23.7",
|
|
39
47
|
"typescript": "^7.0.2"
|
|
40
48
|
},
|