Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: redis client

Sidwebworks705f7fc3eb1a
diff --git a/packages/kv/src/client.ts b/packages/kv/src/client.ts
index 73cc1ddabf..afb52e0bd7 100644
--- a/packages/kv/src/client.ts
+++ b/packages/kv/src/client.ts
@@ -1,8 +1,6 @@
import { REDIS_URL } from "@carbon/auth";
import Redis from "ioredis";
-let redis: Redis;
-
declare global {
var __redis: Redis | undefined;
}
@@ -11,30 +9,18 @@ if (!REDIS_URL) {
throw new Error("REDIS_URL is not defined");
}
-// this is needed because in development we don't want to restart
-// the server with every change, but we want to make sure we don't
-// create a new connection to Redis with every change either.
-if (process.env.VERCEL_ENV === "production") {
- redis = new Redis(REDIS_URL, {
+if (!global.__redis) {
+ global.__redis = new Redis(REDIS_URL, {
maxRetriesPerRequest: 3,
- enableReadyCheck: true,
+ lazyConnect: true, // don't connect until first command
+ enableOfflineQueue: true, // buffer commands while connecting
retryStrategy(times) {
- const delay = Math.min(times * 50, 2000);
- return delay;
+ if (times > 3) return null; // stop retrying, don't hang the lambda
+ return Math.min(times * 50, 2000);
}
});
-} else {
- if (!global.__redis) {
- global.__redis = new Redis(REDIS_URL, {
- maxRetriesPerRequest: 3,
- enableReadyCheck: true,
- retryStrategy(times) {
- const delay = Math.min(times * 50, 2000);
- return delay;
- }
- });
- }
- redis = global.__redis;
}
+const redis = global.__redis;
+
export default redis;