cleaned up redis V2
This commit is contained in:
17
server/src/external/redis/initRedisV2.ts
vendored
17
server/src/external/redis/initRedisV2.ts
vendored
@@ -8,24 +8,17 @@ import {
|
||||
waitForRedisReady,
|
||||
} from "./initRedis.js";
|
||||
import {
|
||||
getRedisV2ConnectionConfig,
|
||||
REDIS_V2_COMMAND_TIMEOUT_MS,
|
||||
supportsUpstashShebangForRedisV2,
|
||||
} from "./initUtils/redisV2Config.js";
|
||||
|
||||
const redisV2Config = getRedisV2ConnectionConfig({
|
||||
cacheV2Url: process.env.CACHE_V2_DRAGONFLY_URL,
|
||||
primaryCacheUrl: process.env.CACHE_URL,
|
||||
currentRegion,
|
||||
instanceName: "dragonfly",
|
||||
export const redisV2: Redis = createRedisConnection({
|
||||
cacheUrl: process.env.CACHE_V2_DRAGONFLY_URL?.trim() || "",
|
||||
region: `${currentRegion}:v2`,
|
||||
supportsUpstashShebang: false,
|
||||
commandTimeout: REDIS_V2_COMMAND_TIMEOUT_MS,
|
||||
});
|
||||
|
||||
export const hasRedisV2Config = Boolean(redisV2Config);
|
||||
|
||||
export const redisV2: Redis = redisV2Config
|
||||
? createRedisConnection(redisV2Config)
|
||||
: redis;
|
||||
|
||||
const alternateInstanceUrls: Partial<Record<RedisV2InstanceName, string>> = {
|
||||
upstash: process.env.CACHE_V2_UPSTASH_URL?.trim() || undefined,
|
||||
redis: process.env.CACHE_V2_REDIS_URL?.trim() || undefined,
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
import {
|
||||
hasRedisV2Config,
|
||||
redisV2,
|
||||
} from "../initRedisV2.js";
|
||||
import { redisV2 } from "../initRedisV2.js";
|
||||
import {
|
||||
createRedisAvailability,
|
||||
type RedisAvailabilitySnapshot,
|
||||
} from "./createRedisAvailability.js";
|
||||
import {
|
||||
getRedisAvailability,
|
||||
shouldUseRedis,
|
||||
} from "./redisAvailability.js";
|
||||
import { getRedisAvailability, shouldUseRedis } from "./redisAvailability.js";
|
||||
import { redis as primaryRedis } from "./redisClientRegistry.js";
|
||||
|
||||
const usesPrimaryRedis = redisV2 === primaryRedis;
|
||||
@@ -18,7 +12,7 @@ const getPrimaryBackedRedisV2Availability = (): RedisAvailabilitySnapshot => {
|
||||
const availability = getRedisAvailability();
|
||||
|
||||
return {
|
||||
configured: hasRedisV2Config,
|
||||
configured: true,
|
||||
state: availability.state,
|
||||
status: availability.status,
|
||||
};
|
||||
@@ -34,7 +28,7 @@ const redisV2Availability = usesPrimaryRedis
|
||||
}
|
||||
: createRedisAvailability({
|
||||
redis: redisV2,
|
||||
hasConfig: hasRedisV2Config,
|
||||
hasConfig: true,
|
||||
logPrefix: "RedisV2",
|
||||
logType: "redis_v2_availability_state_set",
|
||||
});
|
||||
@@ -50,7 +44,7 @@ const {
|
||||
export {
|
||||
getRedisV2Availability,
|
||||
primeRedisV2Monitor,
|
||||
shouldUseRedisV2,
|
||||
startRedisV2Monitor,
|
||||
stopRedisV2Monitor,
|
||||
shouldUseRedisV2,
|
||||
};
|
||||
|
||||
@@ -4,16 +4,14 @@ export const REDIS_V2_COMMAND_TIMEOUT_MS = 1_000;
|
||||
|
||||
export const getRedisV2ConnectionConfig = ({
|
||||
cacheV2Url,
|
||||
primaryCacheUrl,
|
||||
currentRegion,
|
||||
instanceName,
|
||||
}: {
|
||||
cacheV2Url?: string;
|
||||
primaryCacheUrl?: string;
|
||||
currentRegion: string;
|
||||
instanceName: RedisV2InstanceName;
|
||||
}) =>
|
||||
cacheV2Url?.trim() && cacheV2Url.trim() !== primaryCacheUrl?.trim()
|
||||
cacheV2Url?.trim()
|
||||
? {
|
||||
cacheUrl: cacheV2Url.trim(),
|
||||
region: `${currentRegion}:v2`,
|
||||
|
||||
@@ -1,59 +1,7 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import {
|
||||
getRedisV2ConnectionConfig,
|
||||
REDIS_V2_COMMAND_TIMEOUT_MS,
|
||||
supportsUpstashShebangForRedisV2,
|
||||
} from "@/external/redis/initUtils/redisV2Config.js";
|
||||
import { supportsUpstashShebangForRedisV2 } from "@/external/redis/initUtils/redisV2Config.js";
|
||||
|
||||
describe("redis V2 connection config", () => {
|
||||
test("uses a distinct CACHE_V2_DRAGONFLY_URL without the Upstash shebang", () => {
|
||||
expect(
|
||||
getRedisV2ConnectionConfig({
|
||||
cacheV2Url: " redis://v2 ",
|
||||
primaryCacheUrl: "redis://primary",
|
||||
currentRegion: "us-west-2",
|
||||
instanceName: "dragonfly",
|
||||
}),
|
||||
).toEqual({
|
||||
cacheUrl: "redis://v2",
|
||||
region: "us-west-2:v2",
|
||||
supportsUpstashShebang: false,
|
||||
commandTimeout: REDIS_V2_COMMAND_TIMEOUT_MS,
|
||||
});
|
||||
});
|
||||
|
||||
test("uses the Upstash shebang when the upstash instance is selected", () => {
|
||||
expect(
|
||||
getRedisV2ConnectionConfig({
|
||||
cacheV2Url: " redis://v2 ",
|
||||
primaryCacheUrl: "redis://primary",
|
||||
currentRegion: "us-west-2",
|
||||
instanceName: "upstash",
|
||||
}),
|
||||
).toMatchObject({
|
||||
supportsUpstashShebang: true,
|
||||
});
|
||||
});
|
||||
|
||||
test("falls back to primary Redis when the V2 URL is absent or matches primary", () => {
|
||||
expect(
|
||||
getRedisV2ConnectionConfig({
|
||||
cacheV2Url: undefined,
|
||||
primaryCacheUrl: "redis://primary",
|
||||
currentRegion: "us-west-2",
|
||||
instanceName: "dragonfly",
|
||||
}),
|
||||
).toBeNull();
|
||||
expect(
|
||||
getRedisV2ConnectionConfig({
|
||||
cacheV2Url: " redis://primary ",
|
||||
primaryCacheUrl: "redis://primary",
|
||||
currentRegion: "us-west-2",
|
||||
instanceName: "dragonfly",
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
test("enables the Upstash shebang only for the upstash instance", () => {
|
||||
expect(supportsUpstashShebangForRedisV2("upstash")).toBe(true);
|
||||
expect(supportsUpstashShebangForRedisV2("redis")).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user