Skip to content

Commit 5c35312

Browse files
committed
moves out connect from redis cache class
Signed-off-by: Konstantina Blazhukova <konstantina.blajukova@gmail.com>
1 parent 708526f commit 5c35312

File tree

3 files changed

+35
-47
lines changed

3 files changed

+35
-47
lines changed

packages/relay/src/lib/clients/cache/redisCache.ts

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
44
import { Logger } from 'pino';
55
import { Registry } from 'prom-client';
6-
import type { RedisClientType } from 'redis';
6+
import { createClient } from 'redis';
77

88
import { Utils } from '../../../utils';
99
import { RedisCacheError } from '../../errors/RedisCacheError';
@@ -39,47 +39,20 @@ export class RedisCache implements IRedisCacheClient {
3939
* The Redis client.
4040
* @private
4141
*/
42-
private readonly client: RedisClientType;
43-
44-
/**
45-
* Boolean showing if the connection to the Redis client has finished.
46-
* @private
47-
*/
48-
private connected: Promise<boolean>;
42+
private readonly client: ReturnType<typeof createClient>;
4943

5044
/**
5145
* Creates an instance of `RedisCache`.
5246
*
5347
* @param {Logger} logger - The logger instance.
5448
* @param {Registry} register - The metrics registry.
5549
*/
56-
public constructor(logger: Logger, register: Registry, client: RedisClientType) {
50+
public constructor(logger: Logger, register: Registry, client: ReturnType<typeof createClient>) {
5751
this.logger = logger;
5852
this.register = register;
5953
this.client = client;
60-
this.connected = this.client
61-
.connect()
62-
.then(() => true)
63-
.catch((error) => {
64-
this.logger.error(error, 'Redis connection could not be established!');
65-
return false;
66-
});
67-
this.client.on('ready', async () => {
68-
this.connected = Promise.resolve(true);
69-
const connections = await this.getNumberOfConnections().catch((error) => {
70-
this.logger.error(error);
71-
return 0;
72-
});
73-
logger.info(
74-
`Connected to Redis server (${ConfigService.get('REDIS_URL')}) successfully! Number of connections: ${connections}`,
75-
);
76-
});
77-
this.client.on('end', () => {
78-
this.connected = Promise.resolve(false);
79-
logger.info('Disconnected from Redis server!');
80-
});
54+
8155
this.client.on('error', (error) => {
82-
this.connected = Promise.resolve(false);
8356
const redisError = new RedisCacheError(error);
8457
if (redisError.isSocketClosed()) {
8558
logger.error(`Error occurred with Redis Connection when closing socket: ${redisError.message}`);
@@ -89,8 +62,8 @@ export class RedisCache implements IRedisCacheClient {
8962
});
9063
}
9164

92-
async getConnectedClient(): Promise<RedisClientType> {
93-
return this.isConnected().then(() => this.client);
65+
async getConnectedClient(): Promise<ReturnType<typeof createClient>> {
66+
return this.client.isConnected().then(() => this.client);
9467
}
9568

9669
/**

packages/relay/src/lib/relay.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,12 @@ export class Relay {
131131
// Initialize Redis client for cache (DB index defaults to 0)
132132
const redisUrl = ConfigService.get('REDIS_URL')!;
133133
const reconnectDelay = ConfigService.get('REDIS_RECONNECT_DELAY_MS');
134-
const cacheRedisClient: RedisClientType = createClient({
135-
url: redisUrl,
136-
socket: {
137-
reconnectStrategy: (retries: number) => retries * reconnectDelay,
138-
},
139-
});
134+
const redisClient = this.createAndConnectRedisClient(redisUrl, reconnectDelay);
135+
140136
const chainId = ConfigService.get('CHAIN_ID');
141137
const duration = constants.HBAR_RATE_LIMIT_DURATION;
142138
const reservedKeys = HbarSpendingPlanConfigService.getPreconfiguredSpendingPlanKeys(logger);
143-
this.cacheService = new CacheService(
144-
logger.child({ name: 'cache-service' }),
145-
register,
146-
reservedKeys,
147-
cacheRedisClient,
148-
);
139+
this.cacheService = new CacheService(logger.child({ name: 'cache-service' }), register, reservedKeys, redisClient);
149140

150141
const hbarSpendingPlanRepository = new HbarSpendingPlanRepository(
151142
this.cacheService,
@@ -355,4 +346,28 @@ export class Relay {
355346
this.logger.info(`Operator account '${operator}' has balance: ${balance}`);
356347
}
357348
}
349+
350+
private createAndConnectRedisClient(redisUrl: string, reconnectDelay: number): ReturnType<typeof createClient> {
351+
const redisClient = createClient({
352+
url: redisUrl,
353+
socket: {
354+
reconnectStrategy: (retries: number) => retries * reconnectDelay,
355+
},
356+
});
357+
redisClient
358+
.connect()
359+
.then(() => true)
360+
.catch((error) => {
361+
this.logger.error(error, 'Redis connection could not be established!');
362+
return false;
363+
});
364+
redisClient.on('ready', () => {
365+
this.logger.info(`Redis client connected to ${redisUrl}`);
366+
});
367+
redisClient.on('error', (error) => {
368+
this.logger.error(error, 'Redis connection could not be established!');
369+
});
370+
371+
return redisClient;
372+
}
358373
}

packages/relay/src/lib/services/cacheService/cacheService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services';
44
import type { Logger } from 'pino';
55
import { Counter, Registry } from 'prom-client';
6-
import type { RedisClientType } from 'redis';
6+
import { createClient } from 'redis';
77

88
import { LocalLRUCache, RedisCache } from '../../clients';
99
import { ICacheClient } from '../../clients/cache/ICacheClient';
@@ -83,7 +83,7 @@ export class CacheService {
8383
logger: Logger,
8484
register: Registry = new Registry(),
8585
reservedKeys: Set<string> = new Set(),
86-
redisClient?: RedisClientType,
86+
redisClient?: ReturnType<typeof createClient>,
8787
) {
8888
this.logger = logger;
8989
this.register = register;

0 commit comments

Comments
 (0)