slot check before using redis
This commit is contained in:
109
src/index.ts
109
src/index.ts
@@ -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`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let redisClient: RedisClient;
|
||||||
|
if (useRedis) {
|
||||||
logger.info('Connecting to redis');
|
logger.info('Connecting to redis');
|
||||||
const redisClient = new RedisClient(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD);
|
redisClient = new RedisClient(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD);
|
||||||
await redisClient.connect();
|
await redisClient.connect();
|
||||||
|
}
|
||||||
|
|
||||||
logger.info(`Initializing all market subscribers...`);
|
logger.info(`Initializing all market subscribers...`);
|
||||||
const initAllMarketSubscribersStart = Date.now();
|
const initAllMarketSubscribersStart = Date.now();
|
||||||
@@ -751,25 +756,46 @@ const main = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let l2Formatted: any;
|
let l2Formatted: any;
|
||||||
|
if (useRedis) {
|
||||||
if (
|
if (
|
||||||
!isSpot &&
|
!isSpot &&
|
||||||
`${includeVamm}`.toLowerCase() === 'true' &&
|
`${includeVamm}`.toLowerCase() === 'true' &&
|
||||||
`${includeOracle}`.toLowerCase().toLowerCase() === 'true' &&
|
`${includeOracle}`.toLowerCase().toLowerCase() === 'true' &&
|
||||||
!grouping
|
!grouping
|
||||||
) {
|
) {
|
||||||
|
let redisL2: string;
|
||||||
if (parseInt(adjustedDepth as string) === 5) {
|
if (parseInt(adjustedDepth as string) === 5) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_5`
|
`last_update_orderbook_perp_${normedMarketIndex}_depth_5`
|
||||||
);
|
);
|
||||||
|
if (
|
||||||
|
Math.abs(
|
||||||
|
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
|
||||||
|
) < 10
|
||||||
|
)
|
||||||
|
l2Formatted = redisL2;
|
||||||
} else if (parseInt(adjustedDepth as string) === 20) {
|
} else if (parseInt(adjustedDepth as string) === 20) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_20`
|
`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) {
|
} else if (parseInt(adjustedDepth as string) === 100) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
|
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (
|
||||||
|
redisL2 &&
|
||||||
|
Math.abs(
|
||||||
|
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
|
||||||
|
) < 10
|
||||||
|
)
|
||||||
|
l2Formatted = redisL2;
|
||||||
} else if (
|
} else if (
|
||||||
isSpot &&
|
isSpot &&
|
||||||
`${includeSerum}`.toLowerCase() === 'true' &&
|
`${includeSerum}`.toLowerCase() === 'true' &&
|
||||||
@@ -777,20 +803,39 @@ const main = async () => {
|
|||||||
`${includeOracle}`.toLowerCase() === 'true' &&
|
`${includeOracle}`.toLowerCase() === 'true' &&
|
||||||
!grouping
|
!grouping
|
||||||
) {
|
) {
|
||||||
|
let redisL2: string;
|
||||||
if (parseInt(adjustedDepth as string) === 5) {
|
if (parseInt(adjustedDepth as string) === 5) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_5`
|
`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) {
|
} else if (parseInt(adjustedDepth as string) === 20) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
|
`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) {
|
} else if (parseInt(adjustedDepth as string) === 100) {
|
||||||
console.log('100');
|
redisL2 = await redisClient.client.get(
|
||||||
l2Formatted = await redisClient.client.get(
|
|
||||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
|
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (
|
||||||
|
redisL2 &&
|
||||||
|
Math.abs(
|
||||||
|
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
|
||||||
|
) < 10
|
||||||
|
)
|
||||||
|
l2Formatted = redisL2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (l2Formatted) {
|
if (l2Formatted) {
|
||||||
@@ -798,6 +843,7 @@ const main = async () => {
|
|||||||
res.end(JSON.stringify(l2Formatted));
|
res.end(JSON.stringify(l2Formatted));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const l2 = dlobSubscriber.getL2({
|
const l2 = dlobSubscriber.getL2({
|
||||||
marketIndex: normedMarketIndex,
|
marketIndex: normedMarketIndex,
|
||||||
@@ -918,22 +964,36 @@ const main = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let l2Formatted: any;
|
let l2Formatted: any;
|
||||||
|
if (useRedis) {
|
||||||
if (
|
if (
|
||||||
marketType === 'perp' &&
|
marketType === 'perp' &&
|
||||||
normedParam['includeVamm'].toLowerCase() === 'true' &&
|
normedParam['includeVamm'].toLowerCase() === 'true' &&
|
||||||
normedParam['includeOracle'].toLowerCase() === 'true' &&
|
normedParam['includeOracle'].toLowerCase() === 'true' &&
|
||||||
!normedParam['grouping']
|
!normedParam['grouping']
|
||||||
) {
|
) {
|
||||||
|
let redisL2: string;
|
||||||
if (parseInt(adjustedDepth as string) === 5) {
|
if (parseInt(adjustedDepth as string) === 5) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_5`
|
`last_update_orderbook_perp_${normedMarketIndex}_depth_5`
|
||||||
);
|
);
|
||||||
|
if (
|
||||||
|
Math.abs(
|
||||||
|
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
|
||||||
|
) < 10
|
||||||
|
)
|
||||||
|
l2Formatted = redisL2;
|
||||||
} else if (parseInt(adjustedDepth as string) === 20) {
|
} else if (parseInt(adjustedDepth as string) === 20) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_20`
|
`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) {
|
} else if (parseInt(adjustedDepth as string) === 100) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
|
`last_update_orderbook_perp_${normedMarketIndex}_depth_100`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -943,24 +1003,45 @@ const main = async () => {
|
|||||||
normedParam['includeSerum'].toLowerCase() === 'true' &&
|
normedParam['includeSerum'].toLowerCase() === 'true' &&
|
||||||
!normedParam['grouping']
|
!normedParam['grouping']
|
||||||
) {
|
) {
|
||||||
|
let redisL2: string;
|
||||||
if (parseInt(adjustedDepth as string) === 5) {
|
if (parseInt(adjustedDepth as string) === 5) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_5`
|
`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) {
|
} else if (parseInt(adjustedDepth as string) === 20) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_20`
|
`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) {
|
} else if (parseInt(adjustedDepth as string) === 100) {
|
||||||
l2Formatted = await redisClient.client.get(
|
redisL2 = await redisClient.client.get(
|
||||||
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
|
`last_update_orderbook_spot_${normedMarketIndex}_depth_100`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (
|
||||||
|
redisL2 &&
|
||||||
|
Math.abs(
|
||||||
|
parseInt(JSON.parse(redisL2).slot) - slotSource.getSlot()
|
||||||
|
) < 10
|
||||||
|
)
|
||||||
|
l2Formatted = redisL2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (l2Formatted) {
|
if (l2Formatted) {
|
||||||
return l2Formatted;
|
return l2Formatted;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const l2 = dlobSubscriber.getL2({
|
const l2 = dlobSubscriber.getL2({
|
||||||
marketIndex: normedMarketIndex,
|
marketIndex: normedMarketIndex,
|
||||||
|
|||||||
Reference in New Issue
Block a user