djshard 0.1.1 → 0.2.0
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.cjs +31 -7
- package/dist/index.mjs +31 -7
- package/package.json +1 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ROBOT
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -119,6 +119,7 @@ For each `ShardCluster`, the operator:
|
|
|
119
119
|
- creates a StatefulSet and a Secret holding a random bridge token,
|
|
120
120
|
- injects `DJSHARD_BRIDGE_URL`, `DJSHARD_AUTH`, `DJSHARD_ID` and `DISCORD_TOKEN` into the first container,
|
|
121
121
|
- serves the bridge itself and assigns shards by pod ordinal (`mybot-2` gets slot 2),
|
|
122
|
+
- fails over: when a pod has been disconnected for 30 seconds (operator flag `--failover-grace`), its clusters are spread over the remaining pods; when it reconnects it gets them back. Pods only start and stop the clusters that changed hands,
|
|
122
123
|
- rolls every pod when `replicas`, `totalShards` or `shardsPerCluster` changes.
|
|
123
124
|
|
|
124
125
|
Your `manager.ts` is the same as in single-machine mode, because `ClusterManager` picks up the `DJSHARD_*` variables automatically.
|
|
@@ -129,6 +130,8 @@ The operator image is published to `ghcr.io/ro80t/djshard-operator` with every r
|
|
|
129
130
|
|
|
130
131
|
- Run the operator as a single replica. Bot connections live in its memory.
|
|
131
132
|
- During a rolling restart, an old pod and a new pod can briefly own the same shard. Discord drops the older session.
|
|
133
|
+
- Failover assumes a disconnected pod is dead. A pod that is alive but cut off from the operator keeps running its shards while another pod takes them over.
|
|
134
|
+
- The standalone `Bridge` (without Kubernetes) does not fail over.
|
|
132
135
|
- Bridge traffic is plain WebSocket protected only by the shared token. Keep it inside the cluster or private network.
|
|
133
136
|
- A cluster that hangs without exiting is not detected (there is no heartbeat).
|
|
134
137
|
- Identify rate limits (`max_concurrency`) are only respected within one machine, through `spawnDelay`.
|
package/dist/index.cjs
CHANGED
|
@@ -159,7 +159,7 @@ var ClusterManager = class extends node_events.EventEmitter {
|
|
|
159
159
|
}
|
|
160
160
|
/** Runs `script` on this machine's clusters only. */
|
|
161
161
|
evalLocal(script, { context, cluster, timeout } = {}) {
|
|
162
|
-
const targets = [...this.clusters.values()].filter((c) => cluster === void 0 || c.id === cluster);
|
|
162
|
+
const targets = [...this.clusters.values()].filter((c) => cluster === void 0 || c.id === cluster).sort((a, b) => a.id - b.id);
|
|
163
163
|
return Promise.all(targets.map((c) => {
|
|
164
164
|
if (!c.proc || c.proc.killed) return Promise.reject(/* @__PURE__ */ new Error(`cluster ${c.id} is not running`));
|
|
165
165
|
const { id, promise } = c.pending.open(timeout);
|
|
@@ -191,15 +191,23 @@ var ClusterManager = class extends node_events.EventEmitter {
|
|
|
191
191
|
for (const c of this.clusters.values()) this.#stop(c);
|
|
192
192
|
}
|
|
193
193
|
#apply(a, force = false) {
|
|
194
|
-
|
|
194
|
+
const run = this.#queue.catch(() => {}).then(async () => {
|
|
195
195
|
const key = JSON.stringify(a);
|
|
196
196
|
if (key === this.#assignKey && !force) return;
|
|
197
197
|
this.#assignKey = key;
|
|
198
|
-
|
|
198
|
+
const sameLayout = !force && a.totalShards === this.totalShards && a.totalClusters === this.totalClusters;
|
|
199
|
+
const kept = /* @__PURE__ */ new Map();
|
|
200
|
+
for (const c of this.clusters.values()) {
|
|
201
|
+
const want = a.clusters.find((x) => x.id === c.id);
|
|
202
|
+
if (sameLayout && want && String(want.shards) === String(c.shards)) kept.set(c.id, c);
|
|
203
|
+
else this.#stop(c);
|
|
204
|
+
}
|
|
199
205
|
this.clusters.clear();
|
|
206
|
+
for (const [id, c] of kept) this.clusters.set(id, c);
|
|
200
207
|
this.totalShards = a.totalShards;
|
|
201
208
|
this.totalClusters = a.totalClusters;
|
|
202
|
-
|
|
209
|
+
const added = a.clusters.filter((x) => !kept.has(x.id));
|
|
210
|
+
for (const [i, { id, shards }] of added.entries()) {
|
|
203
211
|
if (this.#closed) return;
|
|
204
212
|
if (i > 0) await Bun.sleep(this.#opts.spawnDelay);
|
|
205
213
|
const c = {
|
|
@@ -212,6 +220,8 @@ var ClusterManager = class extends node_events.EventEmitter {
|
|
|
212
220
|
this.#start(c);
|
|
213
221
|
}
|
|
214
222
|
});
|
|
223
|
+
this.#queue = run;
|
|
224
|
+
return run;
|
|
215
225
|
}
|
|
216
226
|
#start(c) {
|
|
217
227
|
const info = {
|
|
@@ -257,7 +267,15 @@ var ClusterManager = class extends node_events.EventEmitter {
|
|
|
257
267
|
auth: b.auth,
|
|
258
268
|
name: b.id
|
|
259
269
|
});
|
|
260
|
-
ws.onmessage = (e) =>
|
|
270
|
+
ws.onmessage = (e) => {
|
|
271
|
+
let m;
|
|
272
|
+
try {
|
|
273
|
+
m = JSON.parse(String(e.data));
|
|
274
|
+
} catch {
|
|
275
|
+
return this.emit("debug", "bridge sent bad json");
|
|
276
|
+
}
|
|
277
|
+
this.#onBridge(m);
|
|
278
|
+
};
|
|
261
279
|
ws.onclose = () => {
|
|
262
280
|
this.#online = false;
|
|
263
281
|
this.#wsPending.rejectAll("bridge disconnected");
|
|
@@ -269,7 +287,7 @@ var ClusterManager = class extends node_events.EventEmitter {
|
|
|
269
287
|
switch (m.t) {
|
|
270
288
|
case "assign":
|
|
271
289
|
this.#online = true;
|
|
272
|
-
this.#apply(m).then(() => this.emit("assigned"));
|
|
290
|
+
this.#apply(m).then(() => this.emit("assigned"), (e) => this.emit("rejected", e));
|
|
273
291
|
break;
|
|
274
292
|
case "eval":
|
|
275
293
|
respond(m.id, () => this.evalLocal(m.script, m), (r) => this.#send(r));
|
|
@@ -386,7 +404,13 @@ var Bridge = class {
|
|
|
386
404
|
}
|
|
387
405
|
#onMessage(ws, m) {
|
|
388
406
|
if (ws.data.slot < 0) {
|
|
389
|
-
if (m.t !== "hello" || typeof m.auth !== "string" || !safeEqual(m.auth, this.opts.auth))
|
|
407
|
+
if (m.t !== "hello" || typeof m.auth !== "string" || !safeEqual(m.auth, this.opts.auth)) {
|
|
408
|
+
send(ws, {
|
|
409
|
+
t: "error",
|
|
410
|
+
message: "unauthorized"
|
|
411
|
+
});
|
|
412
|
+
return ws.close(1008, "unauthorized");
|
|
413
|
+
}
|
|
390
414
|
const prev = this.#sticky.get(m.name);
|
|
391
415
|
const slot = prev !== void 0 && !this.#slots[prev] ? prev : this.#slots.findIndex((s) => !s);
|
|
392
416
|
if (slot < 0) {
|
package/dist/index.mjs
CHANGED
|
@@ -158,7 +158,7 @@ var ClusterManager = class extends EventEmitter {
|
|
|
158
158
|
}
|
|
159
159
|
/** Runs `script` on this machine's clusters only. */
|
|
160
160
|
evalLocal(script, { context, cluster, timeout } = {}) {
|
|
161
|
-
const targets = [...this.clusters.values()].filter((c) => cluster === void 0 || c.id === cluster);
|
|
161
|
+
const targets = [...this.clusters.values()].filter((c) => cluster === void 0 || c.id === cluster).sort((a, b) => a.id - b.id);
|
|
162
162
|
return Promise.all(targets.map((c) => {
|
|
163
163
|
if (!c.proc || c.proc.killed) return Promise.reject(/* @__PURE__ */ new Error(`cluster ${c.id} is not running`));
|
|
164
164
|
const { id, promise } = c.pending.open(timeout);
|
|
@@ -190,15 +190,23 @@ var ClusterManager = class extends EventEmitter {
|
|
|
190
190
|
for (const c of this.clusters.values()) this.#stop(c);
|
|
191
191
|
}
|
|
192
192
|
#apply(a, force = false) {
|
|
193
|
-
|
|
193
|
+
const run = this.#queue.catch(() => {}).then(async () => {
|
|
194
194
|
const key = JSON.stringify(a);
|
|
195
195
|
if (key === this.#assignKey && !force) return;
|
|
196
196
|
this.#assignKey = key;
|
|
197
|
-
|
|
197
|
+
const sameLayout = !force && a.totalShards === this.totalShards && a.totalClusters === this.totalClusters;
|
|
198
|
+
const kept = /* @__PURE__ */ new Map();
|
|
199
|
+
for (const c of this.clusters.values()) {
|
|
200
|
+
const want = a.clusters.find((x) => x.id === c.id);
|
|
201
|
+
if (sameLayout && want && String(want.shards) === String(c.shards)) kept.set(c.id, c);
|
|
202
|
+
else this.#stop(c);
|
|
203
|
+
}
|
|
198
204
|
this.clusters.clear();
|
|
205
|
+
for (const [id, c] of kept) this.clusters.set(id, c);
|
|
199
206
|
this.totalShards = a.totalShards;
|
|
200
207
|
this.totalClusters = a.totalClusters;
|
|
201
|
-
|
|
208
|
+
const added = a.clusters.filter((x) => !kept.has(x.id));
|
|
209
|
+
for (const [i, { id, shards }] of added.entries()) {
|
|
202
210
|
if (this.#closed) return;
|
|
203
211
|
if (i > 0) await Bun.sleep(this.#opts.spawnDelay);
|
|
204
212
|
const c = {
|
|
@@ -211,6 +219,8 @@ var ClusterManager = class extends EventEmitter {
|
|
|
211
219
|
this.#start(c);
|
|
212
220
|
}
|
|
213
221
|
});
|
|
222
|
+
this.#queue = run;
|
|
223
|
+
return run;
|
|
214
224
|
}
|
|
215
225
|
#start(c) {
|
|
216
226
|
const info = {
|
|
@@ -256,7 +266,15 @@ var ClusterManager = class extends EventEmitter {
|
|
|
256
266
|
auth: b.auth,
|
|
257
267
|
name: b.id
|
|
258
268
|
});
|
|
259
|
-
ws.onmessage = (e) =>
|
|
269
|
+
ws.onmessage = (e) => {
|
|
270
|
+
let m;
|
|
271
|
+
try {
|
|
272
|
+
m = JSON.parse(String(e.data));
|
|
273
|
+
} catch {
|
|
274
|
+
return this.emit("debug", "bridge sent bad json");
|
|
275
|
+
}
|
|
276
|
+
this.#onBridge(m);
|
|
277
|
+
};
|
|
260
278
|
ws.onclose = () => {
|
|
261
279
|
this.#online = false;
|
|
262
280
|
this.#wsPending.rejectAll("bridge disconnected");
|
|
@@ -268,7 +286,7 @@ var ClusterManager = class extends EventEmitter {
|
|
|
268
286
|
switch (m.t) {
|
|
269
287
|
case "assign":
|
|
270
288
|
this.#online = true;
|
|
271
|
-
this.#apply(m).then(() => this.emit("assigned"));
|
|
289
|
+
this.#apply(m).then(() => this.emit("assigned"), (e) => this.emit("rejected", e));
|
|
272
290
|
break;
|
|
273
291
|
case "eval":
|
|
274
292
|
respond(m.id, () => this.evalLocal(m.script, m), (r) => this.#send(r));
|
|
@@ -385,7 +403,13 @@ var Bridge = class {
|
|
|
385
403
|
}
|
|
386
404
|
#onMessage(ws, m) {
|
|
387
405
|
if (ws.data.slot < 0) {
|
|
388
|
-
if (m.t !== "hello" || typeof m.auth !== "string" || !safeEqual(m.auth, this.opts.auth))
|
|
406
|
+
if (m.t !== "hello" || typeof m.auth !== "string" || !safeEqual(m.auth, this.opts.auth)) {
|
|
407
|
+
send(ws, {
|
|
408
|
+
t: "error",
|
|
409
|
+
message: "unauthorized"
|
|
410
|
+
});
|
|
411
|
+
return ws.close(1008, "unauthorized");
|
|
412
|
+
}
|
|
389
413
|
const prev = this.#sticky.get(m.name);
|
|
390
414
|
const slot = prev !== void 0 && !this.#slots[prev] ? prev : this.#slots.findIndex((s) => !s);
|
|
391
415
|
if (slot < 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "djshard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Clustered + cross-host sharding for Discord bots on Bun, with a Kubernetes operator",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -46,9 +46,6 @@
|
|
|
46
46
|
"tsdown": "^0.23.7",
|
|
47
47
|
"typescript": "^7.0.2"
|
|
48
48
|
},
|
|
49
|
-
"peerDependencies": {
|
|
50
|
-
"typescript": "^7"
|
|
51
|
-
},
|
|
52
49
|
"engines": {
|
|
53
50
|
"bun": ">=1.2"
|
|
54
51
|
}
|