init redis lazy

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-07-20 20:17:32 +02:00
parent 492141547d
commit f2298a1b05
19 changed files with 134 additions and 86 deletions

View File

@@ -1,11 +1,10 @@
import { logger } from '@/utils/logger';
import { getClientIp, parseIp } from '@/utils/parseIp';
import type { FastifyReply, FastifyRequest } from 'fastify';
import { generateDeviceId } from '@openpanel/common';
import { getSalts } from '@openpanel/db';
import { eventsQueue } from '@openpanel/queue';
import { redis } from '@openpanel/redis';
import { getRedisCache } from '@openpanel/redis';
import type { PostEventPayload } from '@openpanel/sdk';
export async function postEvent(
@@ -38,7 +37,7 @@ export async function postEvent(
});
// this will ensure that we don't have multiple events creating sessions
const locked = await redis.set(
const locked = await getRedisCache().set(
`request:priority:${currentDeviceId}-${previousDeviceId}`,
'locked',
'EX',

View File

@@ -1,6 +1,5 @@
import { validateClerkJwt } from '@/utils/auth';
import type { FastifyReply, FastifyRequest } from 'fastify';
import { escape } from 'sqlstring';
import superjson from 'superjson';
import type * as WebSocket from 'ws';
@@ -13,7 +12,7 @@ import {
TABLE_NAMES,
transformMinimalEvent,
} from '@openpanel/db';
import { redis, redisPub, redisSub } from '@openpanel/redis';
import { getRedisCache, getRedisPub, getRedisSub } from '@openpanel/redis';
import { getProjectAccess } from '@openpanel/trpc';
export function getLiveEventInfo(key: string) {
@@ -36,8 +35,8 @@ export async function testVisitors(
return reply.status(404).send('No event found');
}
event.projectId = req.params.projectId;
redisPub.publish('event:received', superjson.stringify(event));
redis.set(
getRedisPub().publish('event:received', superjson.stringify(event));
getRedisCache().set(
`live:event:${event.projectId}:${Math.random() * 1000}`,
'',
'EX',
@@ -61,7 +60,7 @@ export async function testEvents(
if (!event) {
return reply.status(404).send('No event found');
}
redisPub.publish('event:saved', superjson.stringify(event));
getRedisPub().publish('event:saved', superjson.stringify(event));
reply.status(202).send(event);
}
@@ -77,8 +76,8 @@ export function wsVisitors(
) {
const { params } = req;
redisSub.subscribe('event:received');
redisSub.psubscribe('__key*:expired');
getRedisSub().subscribe('event:received');
getRedisSub().psubscribe('__key*:expired');
const message = (channel: string, message: string) => {
if (channel === 'event:received') {
@@ -99,14 +98,14 @@ export function wsVisitors(
}
};
redisSub.on('message', message);
redisSub.on('pmessage', pmessage);
getRedisSub().on('message', message);
getRedisSub().on('pmessage', pmessage);
connection.socket.on('close', () => {
redisSub.unsubscribe('event:saved');
redisSub.punsubscribe('__key*:expired');
redisSub.off('message', message);
redisSub.off('pmessage', pmessage);
getRedisSub().unsubscribe('event:saved');
getRedisSub().punsubscribe('__key*:expired');
getRedisSub().off('message', message);
getRedisSub().off('pmessage', pmessage);
});
}
@@ -140,7 +139,7 @@ export async function wsProjectEvents(
projectId: params.projectId,
});
redisSub.subscribe(subscribeToEvent);
getRedisSub().subscribe(subscribeToEvent);
const message = async (channel: string, message: string) => {
const event = getSuperJson<IServiceCreateEventPayload>(message);
@@ -159,10 +158,10 @@ export async function wsProjectEvents(
}
};
redisSub.on('message', message as any);
getRedisSub().on('message', message as any);
connection.socket.on('close', () => {
redisSub.unsubscribe(subscribeToEvent);
redisSub.off('message', message as any);
getRedisSub().unsubscribe(subscribeToEvent);
getRedisSub().off('message', message as any);
});
}

View File

@@ -5,7 +5,7 @@ import icoToPng from 'ico-to-png';
import sharp from 'sharp';
import { createHash } from '@openpanel/common';
import { redis } from '@openpanel/redis';
import { getRedisCache } from '@openpanel/redis';
interface GetFaviconParams {
url: string;
@@ -51,7 +51,7 @@ export async function getFavicon(
) {
function sendBuffer(buffer: Buffer, cacheKey?: string) {
if (cacheKey) {
redis.set(`favicon:${cacheKey}`, buffer.toString('base64'));
getRedisCache().set(`favicon:${cacheKey}`, buffer.toString('base64'));
}
reply.type('image/png');
return reply.send(buffer);
@@ -65,7 +65,7 @@ export async function getFavicon(
if (imageExtensions.find((ext) => url.endsWith(ext))) {
const cacheKey = createHash(url, 32);
const cache = await redis.get(`favicon:${cacheKey}`);
const cache = await getRedisCache().get(`favicon:${cacheKey}`);
if (cache) {
return sendBuffer(Buffer.from(cache, 'base64'));
}
@@ -76,7 +76,7 @@ export async function getFavicon(
}
const { hostname } = new URL(url);
const cache = await redis.get(`favicon:${hostname}`);
const cache = await getRedisCache().get(`favicon:${hostname}`);
if (cache) {
return sendBuffer(Buffer.from(cache, 'base64'));
@@ -104,9 +104,9 @@ export async function clearFavicons(
request: FastifyRequest,
reply: FastifyReply
) {
const keys = await redis.keys('favicon:*');
const keys = await getRedisCache().keys('favicon:*');
for (const key of keys) {
await redis.del(key);
await getRedisCache().del(key);
}
return reply.status(404).send('OK');
}

View File

@@ -10,7 +10,7 @@ import { round } from '@openpanel/common';
import { chQuery, db, TABLE_NAMES } from '@openpanel/db';
import type { IServiceClient } from '@openpanel/db';
import { eventsQueue } from '@openpanel/queue';
import { redis, redisPub } from '@openpanel/redis';
import { getRedisCache, getRedisPub } from '@openpanel/redis';
import type { AppRouter } from '@openpanel/trpc';
import { appRouter, createContext } from '@openpanel/trpc';
@@ -98,7 +98,7 @@ const startServer = async () => {
reply.send({ name: 'openpanel sdk api' });
});
fastify.get('/healthcheck', async (request, reply) => {
const redisRes = await withTimings(redis.keys('*'));
const redisRes = await withTimings(getRedisCache().keys('*'));
const dbRes = await withTimings(db.project.findFirst());
const queueRes = await withTimings(eventsQueue.getCompleted());
const chRes = await withTimings(
@@ -150,7 +150,7 @@ const startServer = async () => {
});
// Notify when keys expires
redisPub.config('SET', 'notify-keyspace-events', 'Ex');
getRedisPub().config('SET', 'notify-keyspace-events', 'Ex');
} catch (e) {
logger.error(e, 'Failed to start server');
}