slot check before using redis

This commit is contained in:
Nour Alharithi
2023-12-09 16:49:32 -08:00
parent 5493351f40
commit abc23a01ae

View File

@@ -81,6 +81,7 @@ const rateLimitCallsPerSecond = process.env.RATE_LIMIT_CALLS_PER_SECOND
? parseInt(process.env.RATE_LIMIT_CALLS_PER_SECOND) ? parseInt(process.env.RATE_LIMIT_CALLS_PER_SECOND)
: 1; : 1;
const loadTestAllowed = process.env.ALLOW_LOAD_TEST?.toLowerCase() === 'true'; const loadTestAllowed = process.env.ALLOW_LOAD_TEST?.toLowerCase() === 'true';
const useRedis = process.env.USE_REDIS?.toLowerCase() === 'true';
const logFormat = const logFormat =
':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :req[x-forwarded-for]'; ':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :req[x-forwarded-for]';
@@ -268,6 +269,7 @@ const main = async () => {
'updateReceived', 'updateReceived',
(_pubkey: PublicKey, _slot: number, _dataType: 'raw' | 'decoded') => { (_pubkey: PublicKey, _slot: number, _dataType: 'raw' | 'decoded') => {
setLastReceivedWsMsgTs(Date.now()); setLastReceivedWsMsgTs(Date.now());
// eslint-disable-next-line @typescript-eslint/no-unused-vars
updatesReceivedTotal++; updatesReceivedTotal++;
accountUpdatesCounter.add(1); accountUpdatesCounter.add(1);
} }
@@ -347,9 +349,12 @@ const main = async () => {
`DLOBSubscriber initialized in ${Date.now() - initDlobSubscriberStart} ms` `DLOBSubscriber initialized in ${Date.now() - initDlobSubscriberStart} ms`
); );
logger.info('Connecting to redis'); let redisClient: RedisClient;
const redisClient = new RedisClient(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD); if (useRedis) {
await redisClient.connect(); logger.info('Connecting to redis');
redisClient = new RedisClient(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD);
await redisClient.connect();
}
logger.info(`Initializing all market subscribers...`); logger.info(`Initializing all market subscribers...`);
const initAllMarketSubscribersStart = Date.now(); const initAllMarketSubscribersStart = Date.now();
@@ -751,52 +756,93 @@ const main = async () => {
} }
let l2Formatted: any; let l2Formatted: any;
if ( if (useRedis) {
!isSpot && if (
`${includeVamm}`.toLowerCase() === 'true' && !isSpot &&
`${includeOracle}`.toLowerCase().toLowerCase() === 'true' && `${includeVamm}`.toLowerCase() === 'true' &&
!grouping `${includeOracle}`.toLowerCase().toLowerCase() === 'true' &&
) { !grouping
if (parseInt(adjustedDepth as string) === 5) { ) {
l2Formatted = await redisClient.client.get( let redisL2: string;
`last_update_orderbook_perp_${normedMarketIndex}_depth_5` if (parseInt(adjustedDepth as string) === 5) {
); redisL2 = await redisClient.client.get(
} else if (parseInt(adjustedDepth as string) === 20) { `last_update_orderbook_perp_${normedMarketIndex}_depth_5`
l2Formatted = await redisClient.client.get( );
`last_update_orderbook_perp_${normedMarketIndex}_depth_20` if (
); Math.abs(
} else if (parseInt(adjustedDepth as string) === 100) { parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
l2Formatted = await redisClient.client.get( ) < 10
`last_update_orderbook_perp_${normedMarketIndex}_depth_100` )
); l2Formatted = redisL2;
} else if (parseInt(adjustedDepth as string) === 20) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_perp_${normedMarketIndex}_depth_20`
);
if (
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} else if (parseInt(adjustedDepth as string) === 100) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
);
}
if (
redisL2 &&
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} else if (
isSpot &&
`${includeSerum}`.toLowerCase() === 'true' &&
`${includePhoenix}`.toLowerCase() === 'true' &&
`${includeOracle}`.toLowerCase() === 'true' &&
!grouping
) {
let redisL2: string;
if (parseInt(adjustedDepth as string) === 5) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_5`
);
if (
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} else if (parseInt(adjustedDepth as string) === 20) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
);
if (
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} else if (parseInt(adjustedDepth as string) === 100) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
);
}
if (
redisL2 &&
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} }
} else if (
isSpot &&
`${includeSerum}`.toLowerCase() === 'true' &&
`${includePhoenix}`.toLowerCase() === 'true' &&
`${includeOracle}`.toLowerCase() === 'true' &&
!grouping
) {
if (parseInt(adjustedDepth as string) === 5) {
l2Formatted = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_5`
);
} else if (parseInt(adjustedDepth as string) === 20) {
l2Formatted = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
);
} else if (parseInt(adjustedDepth as string) === 100) {
console.log('100');
l2Formatted = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
);
}
}
if (l2Formatted) { if (l2Formatted) {
res.writeHead(200); res.writeHead(200);
res.end(JSON.stringify(l2Formatted)); res.end(JSON.stringify(l2Formatted));
return; return;
}
} }
const l2 = dlobSubscriber.getL2({ const l2 = dlobSubscriber.getL2({
@@ -918,48 +964,83 @@ const main = async () => {
} }
let l2Formatted: any; let l2Formatted: any;
if ( if (useRedis) {
marketType === 'perp' && if (
normedParam['includeVamm'].toLowerCase() === 'true' && marketType === 'perp' &&
normedParam['includeOracle'].toLowerCase() === 'true' && normedParam['includeVamm'].toLowerCase() === 'true' &&
!normedParam['grouping'] normedParam['includeOracle'].toLowerCase() === 'true' &&
) { !normedParam['grouping']
if (parseInt(adjustedDepth as string) === 5) { ) {
l2Formatted = await redisClient.client.get( let redisL2: string;
`last_update_orderbook_perp_${normedMarketIndex}_depth_5` if (parseInt(adjustedDepth as string) === 5) {
); redisL2 = await redisClient.client.get(
} else if (parseInt(adjustedDepth as string) === 20) { `last_update_orderbook_perp_${normedMarketIndex}_depth_5`
l2Formatted = await redisClient.client.get( );
`last_update_orderbook_perp_${normedMarketIndex}_depth_20` if (
); Math.abs(
} else if (parseInt(adjustedDepth as string) === 100) { parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
l2Formatted = await redisClient.client.get( ) < 10
`last_update_orderbook_perp_${normedMarketIndex}_depth_100` )
); l2Formatted = redisL2;
} else if (parseInt(adjustedDepth as string) === 20) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_perp_${normedMarketIndex}_depth_20`
);
if (
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} else if (parseInt(adjustedDepth as string) === 100) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
);
}
} else if (
marketType === 'spot' &&
normedParam['includePhoenix'].toLowerCase() === 'true' &&
normedParam['includeSerum'].toLowerCase() === 'true' &&
!normedParam['grouping']
) {
let redisL2: string;
if (parseInt(adjustedDepth as string) === 5) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_5`
);
if (
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} else if (parseInt(adjustedDepth as string) === 20) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
);
if (
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} else if (parseInt(adjustedDepth as string) === 100) {
redisL2 = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
);
}
if (
redisL2 &&
Math.abs(
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
) < 10
)
l2Formatted = redisL2;
} }
} else if (
marketType === 'spot' &&
normedParam['includePhoenix'].toLowerCase() === 'true' &&
normedParam['includeSerum'].toLowerCase() === 'true' &&
!normedParam['grouping']
) {
if (parseInt(adjustedDepth as string) === 5) {
l2Formatted = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_5`
);
} else if (parseInt(adjustedDepth as string) === 20) {
l2Formatted = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
);
} else if (parseInt(adjustedDepth as string) === 100) {
l2Formatted = await redisClient.client.get(
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
);
}
}
if (l2Formatted) { if (l2Formatted) {
return l2Formatted; return l2Formatted;
}
} }
const l2 = dlobSubscriber.getL2({ const l2 = dlobSubscriber.getL2({