@mastra/redis-streams 0.8.0 → 0.8.1-alpha.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/dist/index.cjs +62 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -2
- package/dist/index.js.map +1 -1
- package/package.json +15 -14
package/dist/index.js
CHANGED
|
@@ -1,10 +1,32 @@
|
|
|
1
1
|
import { PubSub } from "@mastra/core/events";
|
|
2
|
-
import { createClient, createCluster } from "redis";
|
|
2
|
+
import { ClientOfflineError, ConnectionTimeoutError, SocketClosedUnexpectedlyError, SocketTimeoutError, createClient, createCluster } from "redis";
|
|
3
3
|
// src/index.ts
|
|
4
4
|
/** Page size for the reclaim loop's XPENDING scan and the max entries claimed per tick. */
|
|
5
5
|
const RECLAIM_PAGE_SIZE = 100;
|
|
6
6
|
const DEFAULT_STREAM_IDLE_TTL_MS = 6048e5;
|
|
7
7
|
const TRIM_PAGE_SIZE = 500;
|
|
8
|
+
const PUBLISH_RETRY_BUDGET_MS = 12e4;
|
|
9
|
+
const PUBLISH_RETRY_DELAY_MS = 250;
|
|
10
|
+
const TRANSIENT_SOCKET_CODES = /* @__PURE__ */ new Set([
|
|
11
|
+
"ECONNRESET",
|
|
12
|
+
"EPIPE",
|
|
13
|
+
"ETIMEDOUT",
|
|
14
|
+
"ECONNREFUSED"
|
|
15
|
+
]);
|
|
16
|
+
function isLoadingError(error) {
|
|
17
|
+
return error instanceof Error && error.message.startsWith("LOADING");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Whether a publish failed for a reason that a later attempt can get past: the
|
|
21
|
+
* server is still loading, or the connection dropped. Other server replies
|
|
22
|
+
* (wrong type, OOM, auth) are not retried.
|
|
23
|
+
*/
|
|
24
|
+
function isTransientPublishError(error) {
|
|
25
|
+
if (isLoadingError(error)) return true;
|
|
26
|
+
if (error instanceof SocketClosedUnexpectedlyError || error instanceof ClientOfflineError || error instanceof ConnectionTimeoutError || error instanceof SocketTimeoutError) return true;
|
|
27
|
+
const code = error?.code;
|
|
28
|
+
return typeof code === "string" && TRANSIENT_SOCKET_CODES.has(code);
|
|
29
|
+
}
|
|
8
30
|
/**
|
|
9
31
|
* Heartbeat: reset a pending entry's idle time only if this consumer still
|
|
10
32
|
* owns it, so a late heartbeat never takes an entry back from a sibling.
|
|
@@ -152,6 +174,17 @@ async function disconnect(client) {
|
|
|
152
174
|
}
|
|
153
175
|
await client.quit();
|
|
154
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Thrown when a publish could not reach Redis before the retry budget ran
|
|
179
|
+
* out, including when an attempt never settled (node-redis parks commands in
|
|
180
|
+
* its offline queue while disconnected).
|
|
181
|
+
*/
|
|
182
|
+
var PublishRetryBudgetExhaustedError = class extends Error {
|
|
183
|
+
constructor(budgetMs, options) {
|
|
184
|
+
super(`redis-streams: publish did not reach Redis within ${budgetMs}ms`, options);
|
|
185
|
+
this.name = "PublishRetryBudgetExhaustedError";
|
|
186
|
+
}
|
|
187
|
+
};
|
|
155
188
|
var RedisStreamsPubSub = class RedisStreamsPubSub extends PubSub {
|
|
156
189
|
get supportedModes() {
|
|
157
190
|
return ["pull"];
|
|
@@ -172,6 +205,7 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends PubSub {
|
|
|
172
205
|
#reclaimIdleMs;
|
|
173
206
|
#maxDeliveryAttempts;
|
|
174
207
|
#inFlightTimeoutMs;
|
|
208
|
+
#publishRetryBudgetMs;
|
|
175
209
|
#logger;
|
|
176
210
|
#subscriptions = /* @__PURE__ */ new Map();
|
|
177
211
|
#fanoutReaders = /* @__PURE__ */ new Map();
|
|
@@ -206,6 +240,7 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends PubSub {
|
|
|
206
240
|
const inFlightTimeout = options.inFlightTimeoutMs ?? 0;
|
|
207
241
|
if (!Number.isFinite(inFlightTimeout) || inFlightTimeout < 0) throw new Error(`redis-streams: inFlightTimeoutMs must be a non-negative number (milliseconds), got ${inFlightTimeout}`);
|
|
208
242
|
this.#inFlightTimeoutMs = inFlightTimeout;
|
|
243
|
+
this.#publishRetryBudgetMs = options.publishRetryBudgetMs ?? PUBLISH_RETRY_BUDGET_MS;
|
|
209
244
|
}
|
|
210
245
|
/**
|
|
211
246
|
* Resolve the shared writer client from config. Exactly one connection
|
|
@@ -337,6 +372,28 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends PubSub {
|
|
|
337
372
|
};
|
|
338
373
|
const xaddOptions = this.#xaddTrimOptions();
|
|
339
374
|
const streamKey = this.#streamKey(topic);
|
|
375
|
+
const budgetMs = this.#publishRetryBudgetMs;
|
|
376
|
+
const deadline = Date.now() + budgetMs;
|
|
377
|
+
let lastError;
|
|
378
|
+
for (;;) {
|
|
379
|
+
const remaining = deadline - Date.now();
|
|
380
|
+
if (remaining <= 0) throw lastError ?? new PublishRetryBudgetExhaustedError(budgetMs);
|
|
381
|
+
let timer;
|
|
382
|
+
const budgetExhausted = new Promise((_, reject) => {
|
|
383
|
+
timer = setTimeout(() => reject(new PublishRetryBudgetExhaustedError(budgetMs, { cause: lastError })), remaining);
|
|
384
|
+
});
|
|
385
|
+
try {
|
|
386
|
+
return await Promise.race([this.#xaddEvent(streamKey, payload, xaddOptions), budgetExhausted]);
|
|
387
|
+
} catch (error) {
|
|
388
|
+
if (!isTransientPublishError(error) || this.#closed || Date.now() >= deadline) throw error;
|
|
389
|
+
lastError = error;
|
|
390
|
+
await new Promise((resolve) => setTimeout(resolve, PUBLISH_RETRY_DELAY_MS));
|
|
391
|
+
} finally {
|
|
392
|
+
clearTimeout(timer);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
async #xaddEvent(streamKey, payload, xaddOptions) {
|
|
340
397
|
await (this.#streamIdleTtlMs > 0 ? this.#writeClient.multi().xAdd(streamKey, "*", { event: JSON.stringify(payload) }, xaddOptions).pExpire(streamKey, this.#streamIdleTtlMs).exec().then((replies) => {
|
|
341
398
|
if (Number(replies[1]) !== 1) this.#logger?.warn?.("redis-streams: PEXPIRE inside publish MULTI did not apply", {
|
|
342
399
|
streamKey,
|
|
@@ -836,6 +893,10 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends PubSub {
|
|
|
836
893
|
]
|
|
837
894
|
}) === 1;
|
|
838
895
|
}
|
|
896
|
+
/** A server replaying its dataset after a restart answers `LOADING` to every client. */
|
|
897
|
+
isUnavailableToAll(error) {
|
|
898
|
+
return isLoadingError(error);
|
|
899
|
+
}
|
|
839
900
|
/**
|
|
840
901
|
* Disconnect all clients and stop all subscription loops.
|
|
841
902
|
*/
|
|
@@ -1218,6 +1279,6 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends PubSub {
|
|
|
1218
1279
|
}
|
|
1219
1280
|
}
|
|
1220
1281
|
};
|
|
1221
|
-
export { RedisStreamsPubSub };
|
|
1282
|
+
export { PublishRetryBudgetExhaustedError, RedisStreamsPubSub };
|
|
1222
1283
|
|
|
1223
1284
|
//# sourceMappingURL=index.js.map
|