@mastra/redis-streams 0.8.1-alpha.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 +52 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +53 -10
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -6,12 +6,29 @@ let redis = require("redis");
|
|
|
6
6
|
const RECLAIM_PAGE_SIZE = 100;
|
|
7
7
|
const DEFAULT_STREAM_IDLE_TTL_MS = 6048e5;
|
|
8
8
|
const TRIM_PAGE_SIZE = 500;
|
|
9
|
-
const
|
|
10
|
-
const
|
|
9
|
+
const PUBLISH_RETRY_BUDGET_MS = 12e4;
|
|
10
|
+
const PUBLISH_RETRY_DELAY_MS = 250;
|
|
11
|
+
const TRANSIENT_SOCKET_CODES = /* @__PURE__ */ new Set([
|
|
12
|
+
"ECONNRESET",
|
|
13
|
+
"EPIPE",
|
|
14
|
+
"ETIMEDOUT",
|
|
15
|
+
"ECONNREFUSED"
|
|
16
|
+
]);
|
|
11
17
|
function isLoadingError(error) {
|
|
12
18
|
return error instanceof Error && error.message.startsWith("LOADING");
|
|
13
19
|
}
|
|
14
20
|
/**
|
|
21
|
+
* Whether a publish failed for a reason that a later attempt can get past: the
|
|
22
|
+
* server is still loading, or the connection dropped. Other server replies
|
|
23
|
+
* (wrong type, OOM, auth) are not retried.
|
|
24
|
+
*/
|
|
25
|
+
function isTransientPublishError(error) {
|
|
26
|
+
if (isLoadingError(error)) return true;
|
|
27
|
+
if (error instanceof redis.SocketClosedUnexpectedlyError || error instanceof redis.ClientOfflineError || error instanceof redis.ConnectionTimeoutError || error instanceof redis.SocketTimeoutError) return true;
|
|
28
|
+
const code = error?.code;
|
|
29
|
+
return typeof code === "string" && TRANSIENT_SOCKET_CODES.has(code);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
15
32
|
* Heartbeat: reset a pending entry's idle time only if this consumer still
|
|
16
33
|
* owns it, so a late heartbeat never takes an entry back from a sibling.
|
|
17
34
|
* KEYS[1] stream, ARGV[1] group, ARGV[2] consumer, ARGV[3] entry id
|
|
@@ -158,6 +175,17 @@ async function disconnect(client) {
|
|
|
158
175
|
}
|
|
159
176
|
await client.quit();
|
|
160
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Thrown when a publish could not reach Redis before the retry budget ran
|
|
180
|
+
* out, including when an attempt never settled (node-redis parks commands in
|
|
181
|
+
* its offline queue while disconnected).
|
|
182
|
+
*/
|
|
183
|
+
var PublishRetryBudgetExhaustedError = class extends Error {
|
|
184
|
+
constructor(budgetMs, options) {
|
|
185
|
+
super(`redis-streams: publish did not reach Redis within ${budgetMs}ms`, options);
|
|
186
|
+
this.name = "PublishRetryBudgetExhaustedError";
|
|
187
|
+
}
|
|
188
|
+
};
|
|
161
189
|
var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.PubSub {
|
|
162
190
|
get supportedModes() {
|
|
163
191
|
return ["pull"];
|
|
@@ -178,6 +206,7 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
|
|
|
178
206
|
#reclaimIdleMs;
|
|
179
207
|
#maxDeliveryAttempts;
|
|
180
208
|
#inFlightTimeoutMs;
|
|
209
|
+
#publishRetryBudgetMs;
|
|
181
210
|
#logger;
|
|
182
211
|
#subscriptions = /* @__PURE__ */ new Map();
|
|
183
212
|
#fanoutReaders = /* @__PURE__ */ new Map();
|
|
@@ -212,6 +241,7 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
|
|
|
212
241
|
const inFlightTimeout = options.inFlightTimeoutMs ?? 0;
|
|
213
242
|
if (!Number.isFinite(inFlightTimeout) || inFlightTimeout < 0) throw new Error(`redis-streams: inFlightTimeoutMs must be a non-negative number (milliseconds), got ${inFlightTimeout}`);
|
|
214
243
|
this.#inFlightTimeoutMs = inFlightTimeout;
|
|
244
|
+
this.#publishRetryBudgetMs = options.publishRetryBudgetMs ?? PUBLISH_RETRY_BUDGET_MS;
|
|
215
245
|
}
|
|
216
246
|
/**
|
|
217
247
|
* Resolve the shared writer client from config. Exactly one connection
|
|
@@ -343,12 +373,25 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
|
|
|
343
373
|
};
|
|
344
374
|
const xaddOptions = this.#xaddTrimOptions();
|
|
345
375
|
const streamKey = this.#streamKey(topic);
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
376
|
+
const budgetMs = this.#publishRetryBudgetMs;
|
|
377
|
+
const deadline = Date.now() + budgetMs;
|
|
378
|
+
let lastError;
|
|
379
|
+
for (;;) {
|
|
380
|
+
const remaining = deadline - Date.now();
|
|
381
|
+
if (remaining <= 0) throw lastError ?? new PublishRetryBudgetExhaustedError(budgetMs);
|
|
382
|
+
let timer;
|
|
383
|
+
const budgetExhausted = new Promise((_, reject) => {
|
|
384
|
+
timer = setTimeout(() => reject(new PublishRetryBudgetExhaustedError(budgetMs, { cause: lastError })), remaining);
|
|
385
|
+
});
|
|
386
|
+
try {
|
|
387
|
+
return await Promise.race([this.#xaddEvent(streamKey, payload, xaddOptions), budgetExhausted]);
|
|
388
|
+
} catch (error) {
|
|
389
|
+
if (!isTransientPublishError(error) || this.#closed || Date.now() >= deadline) throw error;
|
|
390
|
+
lastError = error;
|
|
391
|
+
await new Promise((resolve) => setTimeout(resolve, PUBLISH_RETRY_DELAY_MS));
|
|
392
|
+
} finally {
|
|
393
|
+
clearTimeout(timer);
|
|
394
|
+
}
|
|
352
395
|
}
|
|
353
396
|
}
|
|
354
397
|
async #xaddEvent(streamKey, payload, xaddOptions) {
|
|
@@ -1237,6 +1280,7 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
|
|
|
1237
1280
|
}
|
|
1238
1281
|
}
|
|
1239
1282
|
};
|
|
1283
|
+
exports.PublishRetryBudgetExhaustedError = PublishRetryBudgetExhaustedError;
|
|
1240
1284
|
exports.RedisStreamsPubSub = RedisStreamsPubSub;
|
|
1241
1285
|
|
|
1242
1286
|
//# sourceMappingURL=index.cjs.map
|