@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 CHANGED
@@ -6,6 +6,28 @@ 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 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
+ ]);
17
+ function isLoadingError(error) {
18
+ return error instanceof Error && error.message.startsWith("LOADING");
19
+ }
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
+ }
9
31
  /**
10
32
  * Heartbeat: reset a pending entry's idle time only if this consumer still
11
33
  * owns it, so a late heartbeat never takes an entry back from a sibling.
@@ -153,6 +175,17 @@ async function disconnect(client) {
153
175
  }
154
176
  await client.quit();
155
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
+ };
156
189
  var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.PubSub {
157
190
  get supportedModes() {
158
191
  return ["pull"];
@@ -173,6 +206,7 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
173
206
  #reclaimIdleMs;
174
207
  #maxDeliveryAttempts;
175
208
  #inFlightTimeoutMs;
209
+ #publishRetryBudgetMs;
176
210
  #logger;
177
211
  #subscriptions = /* @__PURE__ */ new Map();
178
212
  #fanoutReaders = /* @__PURE__ */ new Map();
@@ -207,6 +241,7 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
207
241
  const inFlightTimeout = options.inFlightTimeoutMs ?? 0;
208
242
  if (!Number.isFinite(inFlightTimeout) || inFlightTimeout < 0) throw new Error(`redis-streams: inFlightTimeoutMs must be a non-negative number (milliseconds), got ${inFlightTimeout}`);
209
243
  this.#inFlightTimeoutMs = inFlightTimeout;
244
+ this.#publishRetryBudgetMs = options.publishRetryBudgetMs ?? PUBLISH_RETRY_BUDGET_MS;
210
245
  }
211
246
  /**
212
247
  * Resolve the shared writer client from config. Exactly one connection
@@ -338,6 +373,28 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
338
373
  };
339
374
  const xaddOptions = this.#xaddTrimOptions();
340
375
  const streamKey = this.#streamKey(topic);
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
+ }
395
+ }
396
+ }
397
+ async #xaddEvent(streamKey, payload, xaddOptions) {
341
398
  await (this.#streamIdleTtlMs > 0 ? this.#writeClient.multi().xAdd(streamKey, "*", { event: JSON.stringify(payload) }, xaddOptions).pExpire(streamKey, this.#streamIdleTtlMs).exec().then((replies) => {
342
399
  if (Number(replies[1]) !== 1) this.#logger?.warn?.("redis-streams: PEXPIRE inside publish MULTI did not apply", {
343
400
  streamKey,
@@ -837,6 +894,10 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
837
894
  ]
838
895
  }) === 1;
839
896
  }
897
+ /** A server replaying its dataset after a restart answers `LOADING` to every client. */
898
+ isUnavailableToAll(error) {
899
+ return isLoadingError(error);
900
+ }
840
901
  /**
841
902
  * Disconnect all clients and stop all subscription loops.
842
903
  */
@@ -1219,6 +1280,7 @@ var RedisStreamsPubSub = class RedisStreamsPubSub extends _mastra_core_events.Pu
1219
1280
  }
1220
1281
  }
1221
1282
  };
1283
+ exports.PublishRetryBudgetExhaustedError = PublishRetryBudgetExhaustedError;
1222
1284
  exports.RedisStreamsPubSub = RedisStreamsPubSub;
1223
1285
 
1224
1286
  //# sourceMappingURL=index.cjs.map