chore: update to common redis + top markers account flag
This commit is contained in:
@@ -1,124 +0,0 @@
|
||||
import Redis, { RedisOptions } from 'ioredis';
|
||||
import { sleep } from './utils';
|
||||
|
||||
export const getRedisClient = (
|
||||
host?: string,
|
||||
port?: string,
|
||||
password?: string,
|
||||
db?: number,
|
||||
opts?: RedisOptions
|
||||
): Redis => {
|
||||
if (host && port) {
|
||||
console.log(`Connecting to configured redis:: ${host}:${port}`);
|
||||
|
||||
const redisClient = new Redis({
|
||||
host: host,
|
||||
port: parseInt(port, 10),
|
||||
password: password,
|
||||
...(opts ?? {}),
|
||||
retryStrategy: (times) => {
|
||||
const delay = Math.min(times * 1000, 10000);
|
||||
console.log(
|
||||
`Reconnecting to Redis in ${delay}ms... (retries: ${times})`
|
||||
);
|
||||
return delay;
|
||||
},
|
||||
reconnectOnError: (err) => {
|
||||
const targetError = 'ECONNREFUSED';
|
||||
if (err.message.includes(targetError)) {
|
||||
console.log(
|
||||
`Redis error: ${targetError}. Attempting to reconnect...`
|
||||
);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
maxRetriesPerRequest: null, // unlimited retries
|
||||
});
|
||||
|
||||
redisClient.on('connect', () => {
|
||||
console.log('Connected to Redis.');
|
||||
});
|
||||
|
||||
redisClient.on('error', (err) => {
|
||||
console.error('Redis error:', err);
|
||||
});
|
||||
|
||||
redisClient.on('reconnecting', () => {
|
||||
console.log('Reconnecting to Redis...');
|
||||
});
|
||||
|
||||
return redisClient;
|
||||
}
|
||||
|
||||
console.log(`Using default redis`);
|
||||
return new Redis({ db });
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapper around the redis client.
|
||||
*
|
||||
* You can hover over the underlying redis client methods for explanations of the methods, but will also include links to DOCS for some important concepts below:
|
||||
*
|
||||
* zRange, zRangeByScore etc.:
|
||||
* - All of the "z" methods are methods that use sorted sets.
|
||||
* - Sorted sets are explained here : https://redis.io/docs/data-types/sorted-sets/
|
||||
*/
|
||||
export class RedisClient {
|
||||
public client: Redis;
|
||||
|
||||
connectionPromise: Promise<void>;
|
||||
|
||||
constructor(
|
||||
host?: string,
|
||||
port?: string,
|
||||
password?: string,
|
||||
db?: number,
|
||||
opts?: RedisOptions
|
||||
) {
|
||||
this.client = getRedisClient(host, port, password, db, opts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Should avoid using this unless necessary.
|
||||
* @returns
|
||||
*/
|
||||
public forceGetClient() {
|
||||
return this.client;
|
||||
}
|
||||
|
||||
public get connected() {
|
||||
return this?.client?.status === 'ready';
|
||||
}
|
||||
|
||||
async connect() {
|
||||
if (this.client.status === 'ready' || this.client.status === 'connect') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.client.status === 'connecting') {
|
||||
await sleep(100);
|
||||
return this.connect(); // recursive call to check again
|
||||
}
|
||||
|
||||
try {
|
||||
await this.client.connect();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.client.disconnect();
|
||||
}
|
||||
|
||||
private assertConnected() {
|
||||
if (!this.connected) {
|
||||
throw 'Redis client not connected';
|
||||
}
|
||||
}
|
||||
|
||||
private getPrefixForMetrics(key: string) {
|
||||
return key.split(':')?.[0];
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,11 @@ import {
|
||||
SerumSubscriber,
|
||||
SpotMarketConfig,
|
||||
SpotMarkets,
|
||||
decodeUser,
|
||||
isVariant,
|
||||
} from '@drift-labs/sdk';
|
||||
import { RedisClient } from '@drift/common';
|
||||
|
||||
import { logger } from './logger';
|
||||
import { NextFunction, Request, Response } from 'express';
|
||||
import FEATURE_FLAGS from './featureFlags';
|
||||
@@ -338,6 +341,27 @@ export const validateDlobQuery = (
|
||||
};
|
||||
};
|
||||
|
||||
export const getAccountFromId = async (
|
||||
userMapClient: RedisClient,
|
||||
topMakers: string[]
|
||||
) => {
|
||||
return Promise.all(
|
||||
topMakers.map(async (accountId) => {
|
||||
const userMap = await userMapClient.getRaw(accountId);
|
||||
if (userMap) {
|
||||
return {
|
||||
accountId,
|
||||
account: decodeUser(Buffer.from(userMap.split('::')[1], 'base64')),
|
||||
};
|
||||
}
|
||||
return {
|
||||
accountId,
|
||||
account: null,
|
||||
};
|
||||
})
|
||||
).then((results) => results.filter((user) => !!user));
|
||||
};
|
||||
|
||||
export function errorHandler(
|
||||
err: Error,
|
||||
_req: Request,
|
||||
|
||||
Reference in New Issue
Block a user