fix(redis): share Garnet endpoint and update database versions

This commit is contained in:
dahyeon committed 2026-11-09 16:30:12 +09:00
1 parent 9117c9c0a4
commit 0c4c4239ec
7 files changed
+157 -231

No files matched your search

@@ -2,10 +2,7 @@ import { Logger, Module } from '@nestjs/common';
import { createClient } from 'redis';
import { SecretModule } from '../../global/secret/secret.module';
import { SecretService } from '../../global/secret/secret.service';
import {
assertDistinctRedisInstances,
resolveRedisClientOptions,
} from '../../global/redis/redis.config';
import { loadRedisClientOptions } from '../../global/redis/redis.config';
import { AUCTION_REDIS_CLIENT } from './auction-redis.constants';
import {
AuctionRedisService,
@@ -14,18 +11,6 @@ import {
import { AuctionRedisCommandService } from './auction-redis-command.service';
import { AuctionRedisStateReaderService } from './auction-redis-state-reader.service';
async function readFirstSecret(
secretService: SecretService,
keys: string[],
): Promise<string | undefined> {
for (const key of keys) {
const value = await secretService.get<string>(key, '');
const trimmed = value.trim();
if (trimmed) return trimmed;
}
return undefined;
}
@Module({
imports: [SecretModule],
providers: [
@@ -35,45 +20,10 @@ async function readFirstSecret(
useFactory: async (
secretService: SecretService,
): Promise<AuctionRedisClient> => {
await secretService.ready();
const [
redisUrl,
redisEndpoint,
auctionRedisUrl,
auctionRedisEndpoint,
auctionRedisUsername,
auctionRedisPassword,
] = await Promise.all([
readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']),
readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']),
readFirstSecret(secretService, ['AUCTION_REDIS_URL']),
readFirstSecret(secretService, ['AUCTION_REDIS_ENDPOINT']),
readFirstSecret(secretService, [
'AUCTION_REDIS_USERNAME',
'AUCTION_REDIS_USERID',
]),
readFirstSecret(secretService, ['AUCTION_REDIS_PASSWORD']),
]);
const redisOptions = resolveRedisClientOptions({
endpoint: redisEndpoint,
endpointEnvName: 'REDIS_ENDPOINT',
url: redisUrl,
urlEnvName: 'REDIS_URL',
});
const auctionRedisOptions = resolveRedisClientOptions({
endpoint: auctionRedisEndpoint,
endpointEnvName: 'AUCTION_REDIS_ENDPOINT',
password: auctionRedisPassword,
url: auctionRedisUrl,
urlEnvName: 'AUCTION_REDIS_URL',
username: auctionRedisUsername,
});
assertDistinctRedisInstances(redisOptions.url, auctionRedisOptions.url);
const logger = new Logger('AuctionRedisClient');
const client = createClient(auctionRedisOptions);
const client = createClient(
await loadRedisClientOptions(secretService),
);
client.on('error', (error: unknown) => {
logger.error(
`Auction Redis client error: ${
@@ -1,12 +1,8 @@
import {
assertDistinctRedisInstances,
normalizeRedisUrl,
resolveRedisClientOptions,
} from './redis.config';
import { normalizeRedisUrl, resolveRedisClientOptions } from './redis.config';
describe('redis.config', () => {
it('normalizes host:port endpoints into redis URLs', () => {
expect(normalizeRedisUrl('127.0.0.1:6379', 'REDIS_ENDPOINT')).toBe(
expect(normalizeRedisUrl(' 127.0.0.1:6379 ')).toBe(
'redis://127.0.0.1:6379',
);
expect(normalizeRedisUrl('rediss://cache.example.com:6380')).toBe(
@@ -15,8 +11,8 @@ describe('redis.config', () => {
});
it('throws a clear error for empty endpoints', () => {
expect(() => normalizeRedisUrl(' ', 'REDIS_ENDPOINT')).toThrow(
'REDIS_ENDPOINT cannot be empty',
expect(() => normalizeRedisUrl(' ')).toThrow(
'GARNET_ENDPOINT cannot be empty',
);
});
@@ -26,34 +22,32 @@ describe('redis.config', () => {
endpoint: 'redis.internal:6379',
username: '',
password: undefined,
endpointEnvName: 'REDIS_ENDPOINT',
}),
).toEqual({
url: 'redis://redis.internal:6379',
});
});
it('rejects auction Redis when it points at the same physical Redis instance', () => {
expect(() =>
assertDistinctRedisInstances(
'redis://cache.internal:6379/0',
'redis://cache.internal:6379/1',
),
).toThrow('AUCTION_REDIS must use a separate Redis instance');
});
it.each([undefined, '', ' '])(
'rejects a missing or blank endpoint: %p',
(endpoint) => {
expect(() => resolveRedisClientOptions({ endpoint })).toThrow(
'GARNET_ENDPOINT cannot be empty',
);
},
);
it('allows different Redis hosts or ports', () => {
expect(() =>
assertDistinctRedisInstances(
'redis://cache.internal:6379',
'redis://auction.internal:6379',
),
).not.toThrow();
expect(() =>
assertDistinctRedisInstances(
'redis://cache.internal:6379',
'redis://cache.internal:6380',
),
).not.toThrow();
it('preserves TLS, database selection, and credentials', () => {
expect(
resolveRedisClientOptions({
endpoint: ' rediss://garnet.internal:6380/1 ',
username: ' garnet-user ',
password: ' garnet-password ',
}),
).toEqual({
url: 'rediss://garnet.internal:6380/1',
username: 'garnet-user',
password: 'garnet-password',
});
});
});
@@ -1,3 +1,5 @@
import type { SecretService } from '../secret/secret.service';
export interface RedisClientOptions {
password?: string;
url: string;
@@ -6,10 +8,7 @@ export interface RedisClientOptions {
export interface ResolveRedisClientOptionsParams {
endpoint?: string;
endpointEnvName?: string;
password?: string;
url?: string;
urlEnvName?: string;
username?: string;
}
@@ -18,13 +17,10 @@ function trimOptional(value: string | undefined): string | undefined {
return trimmed ? trimmed : undefined;
}
export function normalizeRedisUrl(
endpointOrUrl: string,
envName = 'REDIS_URL',
): string {
export function normalizeRedisUrl(endpointOrUrl: string): string {
const trimmed = endpointOrUrl.trim();
if (!trimmed) {
throw new Error(`${envName} cannot be empty`);
throw new Error('GARNET_ENDPOINT cannot be empty');
}
if (/^redis(s)?:\/\//iu.test(trimmed)) {
return trimmed;
@@ -35,19 +31,8 @@ export function normalizeRedisUrl(
export function resolveRedisClientOptions(
params: ResolveRedisClientOptionsParams,
): RedisClientOptions {
const url = trimOptional(params.url);
const endpoint = trimOptional(params.endpoint);
const selectedEndpoint = url ?? endpoint;
const selectedEnvName = url
? (params.urlEnvName ?? 'REDIS_URL')
: (params.endpointEnvName ?? 'REDIS_ENDPOINT');
if (!selectedEndpoint) {
throw new Error(`${selectedEnvName} cannot be empty`);
}
const options: RedisClientOptions = {
url: normalizeRedisUrl(selectedEndpoint, selectedEnvName),
url: normalizeRedisUrl(params.endpoint ?? ''),
};
const username = trimOptional(params.username);
const password = trimOptional(params.password);
@@ -56,22 +41,20 @@ export function resolveRedisClientOptions(
return options;
}
function parseRedisInstanceIdentity(redisUrl: string): string {
const url = new URL(normalizeRedisUrl(redisUrl));
const hostname = url.hostname.toLowerCase();
const port = url.port || '6379';
return `${hostname}:${port}`;
}
export async function loadRedisClientOptions(
secretService: Pick<SecretService, 'get' | 'ready'>,
): Promise<RedisClientOptions> {
await secretService.ready();
const [endpoint, username, userId, password] = await Promise.all([
secretService.get<string>('GARNET_ENDPOINT', ''),
secretService.get<string>('GARNET_USERNAME', ''),
secretService.get<string>('GARNET_USERID', ''),
secretService.get<string>('GARNET_PASSWORD', ''),
]);
export function assertDistinctRedisInstances(
redisUrl: string,
auctionRedisUrl: string,
): void {
const redisIdentity = parseRedisInstanceIdentity(redisUrl);
const auctionRedisIdentity = parseRedisInstanceIdentity(auctionRedisUrl);
if (redisIdentity === auctionRedisIdentity) {
throw new Error(
'AUCTION_REDIS must use a separate Redis instance from REDIS because auction Redis is a live state source of truth.',
);
}
return resolveRedisClientOptions({
endpoint,
password,
username: trimOptional(username) ?? userId,
});
}
@@ -0,0 +1,99 @@
import { Test } from '@nestjs/testing';
import { createClient } from 'redis';
import { AuctionRedisModule } from '../../auction/redis/auction-redis.module';
import { AuctionRedisService } from '../../auction/redis/auction-redis.service';
import { SecretService } from '../secret/secret.service';
import { RedisModule } from './redis.module';
import { RedisService, type RedisClient } from './redis.service';
jest.mock('redis', () => ({ createClient: jest.fn() }));
describe('shared Garnet instance', () => {
it.each([
{
name: 'only GARNET_ENDPOINT is configured',
secrets: { GARNET_ENDPOINT: 'garnet.internal:6379' },
options: { url: 'redis://garnet.internal:6379' },
},
{
name: 'existing Garnet credentials are configured',
secrets: {
GARNET_ENDPOINT: ' rediss://garnet.internal:6380/1 ',
GARNET_USERID: 'garnet-user',
GARNET_PASSWORD: 'garnet-password',
},
options: {
url: 'rediss://garnet.internal:6380/1',
username: 'garnet-user',
password: 'garnet-password',
},
},
{
name: 'obsolete Redis settings are also configured',
secrets: {
GARNET_ENDPOINT: 'garnet.internal:6379',
GARNET_USERNAME: 'garnet-user',
GARNET_PASSWORD: 'garnet-password',
GARNET_URL: 'redis://obsolete-garnet.internal:6379',
REDIS_URL: 'redis://obsolete-common.internal:6379',
REDIS_ENDPOINT: 'obsolete-common.internal:6379',
REDIS_USERNAME: 'obsolete-common-user',
REDIS_PASSWORD: 'obsolete-common-password',
AUCTION_REDIS_URL: 'redis://obsolete-common.internal:6379',
AUCTION_REDIS_ENDPOINT: 'obsolete-common.internal:6379',
AUCTION_REDIS_USERNAME: 'obsolete-auction-user',
AUCTION_REDIS_PASSWORD: 'obsolete-auction-password',
},
options: {
url: 'redis://garnet.internal:6379',
username: 'garnet-user',
password: 'garnet-password',
},
},
])('boots both Redis modules when $name', async ({ secrets, options }) => {
const values: Record<string, string | undefined> = secrets;
const secretService = {
ready: jest.fn().mockResolvedValue(undefined),
get: jest.fn((key: string, fallback: string) =>
Promise.resolve(values[key] ?? fallback),
),
};
const clients = Array.from({ length: 2 }, () => ({
connect: jest.fn().mockResolvedValue(undefined),
on: jest.fn(),
quit: jest.fn().mockResolvedValue(undefined),
destroy: jest.fn(),
}));
const createClientMock = jest.mocked(createClient);
createClientMock.mockReset();
for (const client of clients) {
createClientMock.mockReturnValueOnce(client as unknown as RedisClient);
}
const module = await Test.createTestingModule({
imports: [RedisModule, AuctionRedisModule],
})
.overrideProvider(SecretService)
.useValue(secretService)
.compile();
try {
await module.init();
expect(createClientMock).toHaveBeenCalledTimes(2);
for (let index = 0; index < clients.length; index++) {
expect(createClientMock).toHaveBeenNthCalledWith(index + 1, options);
expect(clients[index].connect).toHaveBeenCalledTimes(1);
}
expect(clients).toContain(module.get(RedisService).raw());
expect(clients).toContain(module.get(AuctionRedisService).raw());
expect(module.get(RedisService).raw()).not.toBe(
module.get(AuctionRedisService).raw(),
);
} finally {
await module.close();
}
for (const client of clients) {
expect(client.quit).toHaveBeenCalledTimes(1);
}
});
});
@@ -3,22 +3,10 @@ import { createClient } from 'redis';
import { SecretModule } from '../secret/secret.module';
import { SecretService } from '../secret/secret.service';
import { REDIS_CLIENT } from './redis.constants';
import { resolveRedisClientOptions } from './redis.config';
import { loadRedisClientOptions } from './redis.config';
import { DistributedLockService } from './distributed-lock.service';
import { RedisService, type RedisClient } from './redis.service';
async function readFirstSecret(
secretService: SecretService,
keys: string[],
): Promise<string | undefined> {
for (const key of keys) {
const value = await secretService.get<string>(key, '');
const trimmed = value.trim();
if (trimmed) return trimmed;
}
return undefined;
}
@Module({
imports: [SecretModule],
providers: [
@@ -28,30 +16,9 @@ async function readFirstSecret(
useFactory: async (
secretService: SecretService,
): Promise<RedisClient> => {
await secretService.ready();
const [url, endpoint, username, password] = await Promise.all([
readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']),
readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']),
readFirstSecret(secretService, [
'REDIS_USERNAME',
'REDIS_USERID',
'GARNET_USERNAME',
'GARNET_USERID',
]),
readFirstSecret(secretService, ['REDIS_PASSWORD', 'GARNET_PASSWORD']),
]);
const logger = new Logger('RedisClient');
const client = createClient(
resolveRedisClientOptions({
endpoint,
endpointEnvName: 'REDIS_ENDPOINT',
password,
url,
urlEnvName: 'REDIS_URL',
username,
}),
await loadRedisClientOptions(secretService),
);
client.on('error', (error: unknown) => {