@dokebi/websocket-hub 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/AI.md CHANGED
@@ -1,4 +1,4 @@
1
- # AI 작업 안내 — @dokebi/websocket-hub 0.1.1
1
+ # AI 작업 안내 — @dokebi/websocket-hub 0.2.0
2
2
 
3
3
  WebSocket 접속자 목록·방·방송·kick·수신 ACK를 관리하는 허브다(옛 `dokebi-sync`를 대체). 보통 **@dokebi/websocket이 단독으로 쓰고**, 앱 코드는 websocket의 `defineSocket`을 쓴다. 직접 쓰는 일은 운영 상태 조회(`status()`, `dokebi-hub-status`)와 봉인 프레임 재생 방어(`nonces()`) 정도다. Node >=24, core ^0.2.0, peer: @dokebi/redis·@dokebi/nats(cluster 모드). 세부는 [README](README.md)다.
4
4
 
@@ -58,7 +58,10 @@ npx dokebi-hub-status --config dokebi.yaml --json # 기계용
58
58
 
59
59
  ## 5. 전달 의미
60
60
 
61
- - broadcast는 대상 연결 전원의 ACK와 Redis 완료 저장 뒤 끝난다. 못 받으면 `retryMs`마다 재전송, 같은 clientId로 재접속하면 못 받은 것을 받는다. 기한을 넘기면 `DELIVERY_TIMEOUT`.
61
+ - broadcast는 대상 연결 전원의 ACK 뒤 끝난다. 못 받으면 `retryMs`마다 재전송, 같은 clientId로 재접속하면 못 받은 것을 받는다. 기한을 넘기면 `DELIVERY_TIMEOUT`.
62
+ - 방송 기록은 **리더 메모리에만** 있다(0.2.0, D-0042). 방송·ACK는 Redis를 거치지 않는다(접속자 등록·변경만 Redis에 저장). 리더가 바뀌면 보낸 서버가 같은 ID와 리더가 알려 준 수신자 목록(`accepted`)으로 재전송한다. **보낸 서버와 리더가 동시에 죽으면 그 방송은 사라진다**(성공 응답 전이라 「성공 뒤 유실 없음」은 유지). 반드시 전달돼야 하는 업무는 @dokebi/job.
63
+ - `maxDeliveries`·`deliveryBytes`는 리더 메모리 상한이다(진행 중 방송의 본문·수신자 목록 + 끝난 방송의 결과를 기한까지). 넘으면 `OUTBOX_FULL`.
64
+ - 0.1.x(Redis outbox)와 0.2.0 서버는 같은 namespace에서 섞이지 않는다(설정 지문 차이로 `CONFIG_MISMATCH`). 판 올림은 namespace의 서버를 함께 바꾼다.
62
65
  - presence 이벤트는 owner(서버)별 전체 목록으로 온다. 재접속·리더 변경 뒤에는 스냅샷과 달라진 owner와 사라진 owner를 다시 알린다.
63
66
  - 실제 소비자 없이 acknowledge하지 않는다. 재전송을 업무의 정확히 한 번으로 설명하지 않는다.
64
67
 
package/README.md CHANGED
@@ -71,7 +71,7 @@ HubConnection: identity(ownerEpoch/connection), join(room), leave(room), updateM
71
71
 
72
72
  기본 lease3000ms/poll500ms/ownerTTL15000ms. maxOwners64, 워커 접속128, 총8192, 프레임1MiB, payload64KiB, pending128, queue4MiB. 전체 snapshot도 프레임 한도를 지킨다. [옵션 스키마](options.schema.json)를 확인한다. poll×3≤lease, ownerTTL≥lease×3을 요구한다.
73
73
 
74
- broadcast는 Redis에 본문·대상 스냅샷을 저장하고 전체 수신 ACK와 완료 저장 뒤 `{authorityEpoch,id,recipients}`를 반환한다. 허브 교체 시 동일 ID로 재전송한다. events 소비자는 실제 수신 확인 뒤 `acknowledge(event.id, event.connections)`를 호출한다. `clientId`는 인증된 기기별 재접속 식별자이며 생략 시 소켓 소실 후 복구할 수 없다. 기본 기한60초·재전송1초, 저장10000건/64MiB(완료 기록 포함). DELIVERY_TIMEOUT/OUTBOX_FULL은 성공이 아니다. 일부 소비 후 실패할 수 있으므로 새 ID로 무조건 재실행하지 않는다. Redis 데이터 손실이나 페이지 재시작의 업무 exactly-once는 제공하지 않는다.
74
+ broadcast는 리더 메모리에 본문·대상 스냅샷을 두고, 대상 전원의 수신 ACK 뒤 `{authorityEpoch,id,recipients}`를 반환한다(0.2.0, [D-0042](../../docs/decisions/D-0042-hub-broadcast-in-memory.md)). 방송·ACK는 Redis를 거치지 않는다. 리더는 정한 수신자 목록을 보낸 서버에 `accepted`로 알리고, 허브 교체 시 보낸 서버가 동일 ID·수신자 목록으로 재전송한다. 보낸 서버와 리더가 함께 죽으면 그 방송은 사라진다(아직 성공 응답 전이다). events 소비자는 실제 수신 확인 뒤 `acknowledge(event.id, event.connections)`를 호출한다. `clientId`는 인증된 기기별 재접속 식별자이며 생략 시 소켓 소실 후 복구할 수 없다. 기본 기한60초·재전송1초, 리더 메모리 상한 10000건/64MiB(완료 기록은 본문 없이 기한까지 포함). DELIVERY_TIMEOUT/OUTBOX_FULL은 성공이 아니다. 일부 소비 후 실패할 수 있으므로 새 ID로 무조건 재실행하지 않는다. 페이지 재시작의 업무 exactly-once는 제공하지 않는다. 0.1.x(Redis outbox)와 0.2.0은 같은 namespace에서 섞이지 않는다(설정 지문이 달라 `CONFIG_MISMATCH`).
75
75
 
76
76
  ## 운영 상태와 재생 방어 (D-0038)
77
77
 
@@ -1,34 +1,51 @@
1
1
  import { type HubOptions } from './options.js';
2
- import type { HubStore } from './store.js';
3
2
  import { type HubRecord } from './types.js';
4
3
  export declare function recipientKey(record: HubRecord): string;
5
- interface Saved {
6
- id: string;
7
- producer: string;
8
- digest: string;
9
- data: string;
10
- recipients: string;
11
- expiresAt: number;
12
- status: 'pending' | 'done' | 'expired';
13
- }
4
+ type Reply = (ok: boolean, recipients: readonly string[]) => void;
14
5
  interface Delivery {
15
- saved: Saved;
16
- remaining: Set<string>;
6
+ readonly id: string;
7
+ readonly producer: string;
8
+ readonly digest: string;
9
+ readonly data: string;
10
+ readonly expiresAt: number;
11
+ readonly recipients: readonly string[];
12
+ readonly remaining: Set<string>;
13
+ readonly bytes: number;
17
14
  lastSent: number;
18
15
  }
16
+ interface Finished {
17
+ readonly producer: string;
18
+ readonly digest: string;
19
+ readonly ok: boolean;
20
+ readonly recipients: readonly string[];
21
+ readonly expiresAt: number;
22
+ readonly bytes: number;
23
+ }
24
+ /**
25
+ * Broadcasts of the current hub leader, kept in its memory (D-0042). A broadcast succeeds only
26
+ * when every recipient acknowledged (D-0025); until then it is resent every retryMs. Nothing is
27
+ * written to Redis: when the leader dies, each producer resends its unfinished broadcasts with
28
+ * the same id and the recipient list this leader announced (`accepted`), and the next leader
29
+ * carries on from there. Finished broadcasts keep a small record (no payload) until their
30
+ * deadline so a resent id is answered with its result instead of being delivered again.
31
+ */
19
32
  export declare class Deliveries {
20
33
  #private;
21
- readonly store: HubStore;
22
- readonly token: string;
23
34
  readonly options: HubOptions;
24
35
  readonly connections: () => readonly HubRecord[];
25
36
  readonly send: (owner: string, frame: Record<string, unknown>) => boolean;
26
37
  readonly pending: Map<string, Delivery>;
27
- readonly waiters: Map<string, Map<string, (ok: boolean, recipients: readonly string[]) => void>>;
28
- constructor(store: HubStore, token: string, options: HubOptions, connections: () => readonly HubRecord[], send: (owner: string, frame: Record<string, unknown>) => boolean);
29
- start(): Promise<void>;
30
- accept(producer: string, frame: Record<string, unknown>, reply: (ok: boolean, recipients: readonly string[]) => void): Promise<void>;
31
- acknowledge(input: unknown, records: readonly HubRecord[]): Promise<void>;
32
- tick(): Promise<void>;
38
+ readonly finished: Map<string, Finished>;
39
+ readonly waiters: Map<string, Map<string, Reply>>;
40
+ constructor(options: HubOptions, connections: () => readonly HubRecord[], send: (owner: string, frame: Record<string, unknown>) => boolean);
41
+ /** Bytes the outbox holds now (payloads and recipient lists), bounded by deliveryBytes. */
42
+ get bytes(): number;
43
+ /**
44
+ * Takes a broadcast from `producer`. `accepted` is told the recipient list once, so the
45
+ * producer can resend it to the next leader; `reply` is told the result.
46
+ */
47
+ accept(producer: string, frame: Record<string, unknown>, reply: Reply, accepted?: (recipients: readonly string[]) => void): void;
48
+ acknowledge(input: unknown, records: readonly HubRecord[]): void;
49
+ tick(): void;
33
50
  }
34
51
  export {};
package/dist/delivery.js CHANGED
@@ -15,96 +15,113 @@ export function recipientKey(record) {
15
15
  recipientKeys.set(record, key);
16
16
  return key;
17
17
  }
18
- // ACK progress may be replayed after failover; only durable completion permits success.
18
+ const RECIPIENT = /^[A-Za-z0-9_-]{43}$/;
19
+ /** Remembered bytes of one broadcast besides its payload: ids, digest and bookkeeping. */
20
+ const OVERHEAD = 256;
21
+ /** A pending broadcast holds its recipient keys in a list and a set of those still missing. */
22
+ const PER_RECIPIENT = 48;
23
+ /**
24
+ * A finished broadcast keeps only the list, whose keys are the strings already cached for the
25
+ * connection records (recipientKey): about a pointer each.
26
+ */
27
+ const PER_FINISHED_RECIPIENT = 8;
28
+ /**
29
+ * Broadcasts of the current hub leader, kept in its memory (D-0042). A broadcast succeeds only
30
+ * when every recipient acknowledged (D-0025); until then it is resent every retryMs. Nothing is
31
+ * written to Redis: when the leader dies, each producer resends its unfinished broadcasts with
32
+ * the same id and the recipient list this leader announced (`accepted`), and the next leader
33
+ * carries on from there. Finished broadcasts keep a small record (no payload) until their
34
+ * deadline so a resent id is answered with its result instead of being delivered again.
35
+ */
19
36
  export class Deliveries {
20
- store;
21
- token;
22
37
  options;
23
38
  connections;
24
39
  send;
25
40
  pending = new Map();
41
+ finished = new Map();
26
42
  waiters = new Map();
27
- constructor(store, token, options, connections, send) {
28
- this.store = store;
29
- this.token = token;
43
+ #bytes = 0;
44
+ constructor(options, connections, send) {
30
45
  this.options = options;
31
46
  this.connections = connections;
32
47
  this.send = send;
33
48
  }
34
- async start() {
35
- for (const raw of (await this.store.run('deliveries', this.token))) {
36
- const saved = this.#parse(raw);
37
- if (saved.status === 'pending')
38
- this.#remember(saved);
39
- }
40
- }
41
- #parse(raw) {
42
- const value = object(JSON.parse(raw));
43
- epoch(value.id);
44
- epoch(value.producer);
45
- sequence(value.expiresAt);
46
- if (typeof value.digest !== 'string' ||
47
- !/^[a-f0-9]{64}$/.test(value.digest) ||
48
- !['pending', 'done', 'expired'].includes(String(value.status)) ||
49
- typeof value.recipients !== 'string')
50
- throw new HubError('CHECKPOINT_INVALID');
51
- payload(value.data, this.options.maxPayloadBytes);
52
- const recipients = array(JSON.parse(value.recipients), this.options.maxTotalConnections);
53
- if (new Set(recipients).size !== recipients.length ||
54
- recipients.some((key) => typeof key !== 'string' || !/^[A-Za-z0-9_-]{43}$/.test(key)))
55
- throw new HubError('CHECKPOINT_INVALID');
56
- return value;
49
+ /** Bytes the outbox holds now (payloads and recipient lists), bounded by deliveryBytes. */
50
+ get bytes() {
51
+ return this.#bytes;
57
52
  }
58
- #remember(saved) {
59
- const value = {
60
- saved,
61
- remaining: new Set(JSON.parse(saved.recipients)),
62
- lastSent: 0,
63
- };
64
- this.pending.set(saved.id, value);
65
- return value;
66
- }
67
- async accept(producer, frame, reply) {
53
+ /**
54
+ * Takes a broadcast from `producer`. `accepted` is told the recipient list once, so the
55
+ * producer can resend it to the next leader; `reply` is told the result.
56
+ */
57
+ accept(producer, frame, reply, accepted = () => { }) {
68
58
  const id = epoch(frame.messageId), filter = query(frame.query);
69
59
  payload(frame.data, this.options.maxPayloadBytes);
70
- const expiresAt = sequence(frame.expiresAt);
60
+ const expiresAt = sequence(frame.expiresAt), now = Date.now();
61
+ if (expiresAt <= now || expiresAt > now + this.options.deliveryTimeoutMs + 5000)
62
+ throw new HubError('DELIVERY_TIMEOUT');
63
+ const data = String(frame.data);
71
64
  const digest = createHash('sha256')
72
- .update(JSON.stringify([filter, frame.data, expiresAt]))
65
+ .update(JSON.stringify([filter, data, expiresAt]))
73
66
  .digest('hex');
74
- const recipients = [
75
- ...new Set(this.connections()
76
- .filter((r) => matches(r, filter))
77
- .map(recipientKey)),
78
- ];
79
- const saved = this.#parse(String(await this.store.run('put-delivery', this.token, JSON.stringify({
80
- timeout: this.options.deliveryTimeoutMs,
81
- count: this.options.maxDeliveries,
82
- bytes: this.options.deliveryBytes,
83
- }), JSON.stringify({
84
- id,
85
- producer,
86
- digest,
87
- data: frame.data,
88
- recipients: JSON.stringify(recipients),
89
- expiresAt,
90
- status: 'pending',
91
- }), id)));
92
- if (saved.status !== 'pending') {
93
- reply(saved.status === 'done', JSON.parse(saved.recipients));
67
+ const done = this.finished.get(id);
68
+ if (done) {
69
+ if (done.digest !== digest || done.producer !== producer)
70
+ throw new HubError('ID_CONFLICT');
71
+ reply(done.ok, done.recipients);
94
72
  return;
95
73
  }
74
+ let value = this.pending.get(id);
75
+ if (value) {
76
+ if (value.digest !== digest || value.producer !== producer)
77
+ throw new HubError('ID_CONFLICT');
78
+ }
79
+ else {
80
+ const recipients = frame.recipients === undefined
81
+ ? [
82
+ ...new Set(this.connections()
83
+ .filter((r) => matches(r, filter))
84
+ .map(recipientKey)),
85
+ ]
86
+ : this.#recipients(frame.recipients);
87
+ const bytes = Buffer.byteLength(data) + recipients.length * PER_RECIPIENT + OVERHEAD;
88
+ if (this.pending.size + this.finished.size >= this.options.maxDeliveries ||
89
+ this.#bytes + bytes > this.options.deliveryBytes)
90
+ throw new HubError('OUTBOX_FULL');
91
+ value = {
92
+ id,
93
+ producer,
94
+ digest,
95
+ data,
96
+ expiresAt,
97
+ recipients: Object.freeze(recipients),
98
+ remaining: new Set(recipients),
99
+ bytes,
100
+ lastSent: 0,
101
+ };
102
+ this.pending.set(id, value);
103
+ this.#bytes += bytes;
104
+ }
96
105
  const waiters = this.waiters.get(id) ?? new Map();
97
106
  waiters.set(producer, reply);
98
107
  this.waiters.set(id, waiters);
99
- const value = this.pending.get(id) ?? this.#remember(saved);
108
+ accepted(value.recipients);
100
109
  if (!value.remaining.size)
101
- await this.#finish(value, true);
110
+ this.#finish(value, true);
102
111
  else
103
112
  this.#transmit(value, this.connections());
104
113
  }
114
+ /** A recipient list resent by a producer after a leader change: well-formed keys only. */
115
+ #recipients(input) {
116
+ const recipients = array(input, this.options.maxTotalConnections);
117
+ if (new Set(recipients).size !== recipients.length ||
118
+ recipients.some((key) => typeof key !== 'string' || !RECIPIENT.test(key)))
119
+ throw new HubError('INVALID_ARGUMENT');
120
+ return recipients;
121
+ }
105
122
  #transmit(value, records) {
106
123
  const now = Date.now();
107
- if (now >= value.saved.expiresAt || now - value.lastSent < this.options.retryMs)
124
+ if (now >= value.expiresAt || now - value.lastSent < this.options.retryMs)
108
125
  return;
109
126
  value.lastSent = now;
110
127
  const targets = new Map();
@@ -118,13 +135,13 @@ export class Deliveries {
118
135
  for (const [owner, connections] of targets)
119
136
  this.send(owner, {
120
137
  kind: 'message',
121
- id: value.saved.id,
122
- data: value.saved.data,
123
- expiresAt: value.saved.expiresAt,
138
+ id: value.id,
139
+ data: value.data,
140
+ expiresAt: value.expiresAt,
124
141
  connections,
125
142
  });
126
143
  }
127
- async acknowledge(input, records) {
144
+ acknowledge(input, records) {
128
145
  const identities = new Map(records.map((r) => [r.connection, recipientKey(r)]));
129
146
  const completed = new Set();
130
147
  for (const raw of array(input, 256)) {
@@ -140,24 +157,40 @@ export class Deliveries {
140
157
  completed.add(delivery);
141
158
  }
142
159
  for (const delivery of completed)
143
- await this.#finish(delivery, Date.now() < delivery.saved.expiresAt);
160
+ this.#finish(delivery, Date.now() < delivery.expiresAt);
144
161
  }
145
- async #finish(value, ok) {
146
- const saved = { ...value.saved, data: '', status: ok ? 'done' : 'expired' };
147
- await this.store.run('finish-delivery', this.token, '', JSON.stringify(saved), saved.id);
148
- this.pending.delete(saved.id);
149
- const waiters = this.waiters.get(saved.id);
150
- this.waiters.delete(saved.id);
162
+ #finish(value, ok) {
163
+ this.pending.delete(value.id);
164
+ this.#bytes -= value.bytes;
165
+ // The payload is dropped; the result stays until the deadline for producers that resend.
166
+ const bytes = value.recipients.length * PER_FINISHED_RECIPIENT + OVERHEAD;
167
+ this.finished.set(value.id, {
168
+ producer: value.producer,
169
+ digest: value.digest,
170
+ ok,
171
+ recipients: value.recipients,
172
+ expiresAt: value.expiresAt,
173
+ bytes,
174
+ });
175
+ this.#bytes += bytes;
176
+ const waiters = this.waiters.get(value.id);
177
+ this.waiters.delete(value.id);
151
178
  for (const reply of waiters?.values() ?? [])
152
- reply(ok, JSON.parse(saved.recipients));
179
+ reply(ok, value.recipients);
153
180
  }
154
- async tick() {
181
+ tick() {
155
182
  const records = this.connections(), now = Date.now();
156
183
  for (const value of this.pending.values()) {
157
- if (now >= value.saved.expiresAt)
158
- await this.#finish(value, false);
184
+ if (now >= value.expiresAt)
185
+ this.#finish(value, false);
159
186
  else
160
187
  this.#transmit(value, records);
161
188
  }
189
+ // Producers stop resending at the deadline; keep results a little longer for late replies.
190
+ for (const [id, done] of this.finished)
191
+ if (done.expiresAt + 5000 < now) {
192
+ this.finished.delete(id);
193
+ this.#bytes -= done.bytes;
194
+ }
162
195
  }
163
196
  }
package/dist/index.js CHANGED
@@ -48,7 +48,7 @@ class HubRuntime {
48
48
  /** Factory input: `{ app, options? }`. Options default to `app.config['dokebi-websocket-hub']`. */
49
49
  export const createPlugin = definePlugin({
50
50
  name: 'dokebi-websocket-hub',
51
- version: '0.1.1',
51
+ version: '0.2.0',
52
52
  core: '^0.2.0',
53
53
  options: parseOptions,
54
54
  // local: nothing. cluster: Redis, and NATS only when natsConnection is set (D-0029).
@@ -60,7 +60,7 @@ export const createPlugin = definePlugin({
60
60
  /** Use with `context.getDependency(dependency)`; a registered `dokebi-websocket-hub` instance is reused. */
61
61
  export const dependency = providerDependency({
62
62
  name: 'dokebi-websocket-hub',
63
- version: '^0.1.0',
63
+ version: '^0.2.0',
64
64
  packageName: '@dokebi/websocket-hub',
65
65
  load: () => import('./index.js'),
66
66
  });
package/dist/leader.js CHANGED
@@ -21,7 +21,7 @@ export class Leader {
21
21
  this.identity = identity;
22
22
  this.store = store;
23
23
  this.options = options;
24
- this.#deliveries = new Deliveries(store, identity.epoch, options, () => [...this.#owners.values()].flatMap((owner) => owner.connections), (owner, frame) => {
24
+ this.#deliveries = new Deliveries(options, () => [...this.#owners.values()].flatMap((owner) => owner.connections), (owner, frame) => {
25
25
  const peer = this.#peers.get(owner);
26
26
  if (!peer)
27
27
  return false;
@@ -44,7 +44,6 @@ export class Leader {
44
44
  this.#owners = snapshot.owners;
45
45
  this.#revision = snapshot.revision;
46
46
  await this.#expire(snapshot.now);
47
- await this.#deliveries.start();
48
47
  }
49
48
  async #renew() {
50
49
  const start = performance.now();
@@ -77,6 +76,12 @@ export class Leader {
77
76
  }
78
77
  const peer = {
79
78
  wire: new Wire(socket, this.options.maxFrameBytes, this.options.queueBytes, (frame) => {
79
+ // Broadcasts and ACKs stay in memory (D-0042): no Redis round trip and no wait behind
80
+ // owner updates. The lease is checked against its local deadline.
81
+ if (frame.op === 'broadcast' || frame.op === 'ack') {
82
+ this.#deliver(peer, frame);
83
+ return;
84
+ }
80
85
  void this.#queue(() => this.#handle(peer, frame)).catch((error) => {
81
86
  try {
82
87
  peer.wire.send({
@@ -106,6 +111,64 @@ export class Leader {
106
111
  this.#sockets.add(peer);
107
112
  socket.setTimeout(this.options.ownerTtlMs, () => socket.destroy());
108
113
  }
114
+ #deliver(peer, f) {
115
+ let id;
116
+ try {
117
+ id = integer(f.id, 1, Number.MAX_SAFE_INTEGER);
118
+ if (!this.active)
119
+ throw new HubError('STALE_LEADER');
120
+ const ownerId = peer.owner;
121
+ if (!ownerId || this.#peers.get(ownerId) !== peer || f.epoch !== this.identity.epoch)
122
+ throw new HubError('STALE_PEER');
123
+ const owner = this.#owners.get(ownerId);
124
+ if (!owner || owner.session !== peer.session)
125
+ throw new HubError('STALE_PEER');
126
+ if (f.op === 'ack') {
127
+ this.#deliveries.acknowledge(f.acks, owner.connections);
128
+ this.#send(peer, { kind: 'reply', id, ok: true, result: {} });
129
+ return;
130
+ }
131
+ const reply = id;
132
+ this.#deliveries.accept(ownerId, f, (ok, recipients) => {
133
+ try {
134
+ this.#send(peer, {
135
+ kind: 'reply',
136
+ id: reply,
137
+ ok,
138
+ ...(ok
139
+ ? { result: { authorityEpoch: this.identity.epoch, id: f.messageId, recipients } }
140
+ : { code: 'DELIVERY_TIMEOUT' }),
141
+ });
142
+ }
143
+ catch {
144
+ peer.wire.close();
145
+ }
146
+ },
147
+ // The producer keeps the recipient list to resend it to the next leader.
148
+ (recipients) => {
149
+ try {
150
+ this.#send(peer, { kind: 'accepted', id: f.messageId, recipients });
151
+ }
152
+ catch {
153
+ peer.wire.close();
154
+ }
155
+ });
156
+ }
157
+ catch (error) {
158
+ try {
159
+ peer.wire.send({
160
+ kind: 'reply',
161
+ id,
162
+ epoch: this.identity.epoch,
163
+ ok: false,
164
+ code: error instanceof HubError ? error.code : 'PROTOCOL_ERROR',
165
+ });
166
+ }
167
+ catch { }
168
+ if (!peer.owner || !this.active)
169
+ peer.wire.close();
170
+ }
171
+ }
109
172
  #send(peer, value) {
110
173
  if (!this.active)
111
174
  throw new HubError('STALE_LEADER');
@@ -216,27 +279,6 @@ export class Leader {
216
279
  await this.#remove(ownerId, owner.session);
217
280
  this.#peers.delete(ownerId);
218
281
  }
219
- else if (f.op === 'ack') {
220
- await this.#deliveries.acknowledge(f.acks, owner.connections);
221
- }
222
- else if (f.op === 'broadcast') {
223
- await this.#deliveries.accept(ownerId, f, (ok, recipients) => {
224
- try {
225
- this.#send(peer, {
226
- kind: 'reply',
227
- id,
228
- ok,
229
- ...(ok
230
- ? { result: { authorityEpoch: this.identity.epoch, id: f.messageId, recipients } }
231
- : { code: 'DELIVERY_TIMEOUT' }),
232
- });
233
- }
234
- catch {
235
- peer.wire.close();
236
- }
237
- });
238
- return;
239
- }
240
282
  else if (f.op === 'kick') {
241
283
  const filter = query(f.query), messageId = randomUUID();
242
284
  const code = f.op === 'kick' ? integer(f.code, 4000, 4999) : undefined;
@@ -277,7 +319,7 @@ export class Leader {
277
319
  return this.#queue(async () => {
278
320
  const now = await this.#renew();
279
321
  await this.#expire(now);
280
- await this.#deliveries.tick();
322
+ this.#deliveries.tick();
281
323
  });
282
324
  }
283
325
  async stop() {
@@ -19,6 +19,8 @@ export class PeerClient {
19
19
  #peers = new Map();
20
20
  #revision = 0;
21
21
  #broadcasts = 0;
22
+ /** Unfinished broadcasts by message id; the leader's recipient list is added when accepted. */
23
+ #outgoing = new Map();
22
24
  #acks = new Map();
23
25
  #ackBytes = 0;
24
26
  #ackTimer;
@@ -176,6 +178,18 @@ export class PeerClient {
176
178
  pending.reject(new HubError(typeof f.code === 'string' ? f.code : 'PROTOCOL_ERROR'));
177
179
  return;
178
180
  }
181
+ if (f.kind === 'accepted') {
182
+ // D-0042: the leader keeps broadcasts in memory only. The recipient list it chose rides
183
+ // along when this broadcast is resent to the next leader.
184
+ const body = this.#outgoing.get(epoch(f.id));
185
+ if (body && body.recipients === undefined)
186
+ body.recipients = array(f.recipients, this.options.maxTotalConnections).map((key) => {
187
+ if (typeof key !== 'string' || !/^[A-Za-z0-9_-]{43}$/.test(key))
188
+ throw new HubError('PROTOCOL_ERROR');
189
+ return key;
190
+ });
191
+ return;
192
+ }
179
193
  if (f.kind === 'presence') {
180
194
  const rev = sequence(f.revision);
181
195
  if (!this.#ready) {
@@ -340,12 +354,14 @@ export class PeerClient {
340
354
  messageId: randomUUID(),
341
355
  expiresAt: Date.now() + this.options.deliveryTimeoutMs,
342
356
  };
357
+ const expiresAt = body.expiresAt;
358
+ this.#outgoing.set(body.messageId, body);
343
359
  return (async () => {
344
360
  try {
345
- while (!this.#closing && Date.now() < body.expiresAt) {
361
+ while (!this.#closing && Date.now() < expiresAt) {
346
362
  try {
347
363
  if (this.ready)
348
- return (await this.#rpc('broadcast', body, Math.max(1, body.expiresAt - Date.now())));
364
+ return (await this.#rpc('broadcast', body, Math.max(1, expiresAt - Date.now())));
349
365
  }
350
366
  catch (error) {
351
367
  if (!(error instanceof HubError) ||
@@ -360,12 +376,13 @@ export class PeerClient {
360
376
  ].includes(error.code))
361
377
  throw error;
362
378
  }
363
- await delay(Math.min(100, Math.max(1, body.expiresAt - Date.now())));
379
+ await delay(Math.min(100, Math.max(1, expiresAt - Date.now())));
364
380
  }
365
381
  throw new HubError(this.#closing ? 'CLOSED' : 'DELIVERY_TIMEOUT');
366
382
  }
367
383
  finally {
368
384
  this.#broadcasts--;
385
+ this.#outgoing.delete(body.messageId);
369
386
  }
370
387
  })();
371
388
  }
package/dist/store.js CHANGED
@@ -35,32 +35,6 @@ if op=='leave' then
35
35
  redis.call('HDEL',KEYS[2],ARGV[5]);return redis.call('INCR',KEYS[3])
36
36
  end
37
37
  if not leader or cjson.decode(leader).epoch~=ARGV[2] then return {'hub-error','STALE_LEADER'} end
38
- if op=='deliveries' or op=='put-delivery' or op=='finish-delivery' then
39
- local expired=redis.call('ZRANGEBYSCORE',KEYS[6],'-inf',now,'LIMIT',0,256)
40
- for _,id in ipairs(expired) do
41
- local old=redis.call('HGET',KEYS[5],id)
42
- if old then redis.call('DECRBY',KEYS[7],string.len(old));redis.call('HDEL',KEYS[5],id) end
43
- redis.call('ZREM',KEYS[6],id)
44
- end
45
- if op=='deliveries' then return redis.call('HVALS',KEYS[5]) end
46
- local value=cjson.decode(ARGV[4]);local old=redis.call('HGET',KEYS[5],ARGV[5])
47
- if op=='put-delivery' then
48
- if old then
49
- local saved=cjson.decode(old)
50
- if saved.digest~=value.digest or saved.producer~=value.producer then return {'hub-error','ID_CONFLICT'} end
51
- return old
52
- end
53
- local limits=cjson.decode(ARGV[3])
54
- if value.expiresAt<=now or value.expiresAt>now+limits.timeout+5000 then return {'hub-error','DELIVERY_TIMEOUT'} end
55
- if redis.call('HLEN',KEYS[5])>=limits.count or tonumber(redis.call('GET',KEYS[7]) or '0')+string.len(ARGV[4])>limits.bytes then return {'hub-error','OUTBOX_FULL'} end
56
- else
57
- if not old then return {'hub-error','DELIVERY_TIMEOUT'} end
58
- if value.status=='done' and value.expiresAt<=now then return {'hub-error','DELIVERY_TIMEOUT'} end
59
- end
60
- redis.call('HSET',KEYS[5],ARGV[5],ARGV[4]);redis.call('ZADD',KEYS[6],value.expiresAt+60000,ARGV[5])
61
- redis.call('INCRBY',KEYS[7],string.len(ARGV[4])-(old and string.len(old) or 0))
62
- return ARGV[4]
63
- end
64
38
  if op=='renew' then redis.call('PEXPIRE',KEYS[1],ARGV[3]);return now end
65
39
  if op=='release' then redis.call('DEL',KEYS[1]);return now end
66
40
  if op=='snapshot' then return {redis.call('GET',KEYS[3]) or '0',redis.call('HGETALL',KEYS[2]),now} end
@@ -94,18 +68,12 @@ export class HubStore {
94
68
  const hash = createHash('sha256').update(options.namespace).digest('hex');
95
69
  const prefix = `dokebi:websocket-hub:{${hash}}`;
96
70
  this.#prefix = prefix;
97
- this.keys = [
98
- 'leader',
99
- 'owners',
100
- 'revision',
101
- 'config',
102
- 'deliveries',
103
- 'delivery-expiry',
104
- 'delivery-bytes',
105
- ].map((x) => `${prefix}:${x}`);
71
+ this.keys = ['leader', 'owners', 'revision', 'config'].map((x) => `${prefix}:${x}`);
106
72
  this.fingerprint = createHash('sha256')
107
73
  .update(JSON.stringify([
108
- 'delivery-ack-v1',
74
+ // D-0042: broadcasts live in leader memory; servers of the Redis-outbox version must
75
+ // not share a namespace with this one.
76
+ 'delivery-memory-v2',
109
77
  options.secret,
110
78
  options.leaseMs,
111
79
  options.ownerTtlMs,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/websocket-hub",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "MIT",