Skip to content

Commit

Permalink
Merge pull request #30 from drift-labs/wphan/optimistic_health_check
Browse files Browse the repository at this point in the history
make health check more optimistic, remove userStatsMap
  • Loading branch information
wphan authored Nov 29, 2023
2 parents ff83db8 + d1f2ad3 commit 020e075
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
11 changes: 6 additions & 5 deletions src/core/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,10 @@ const handleHealthCheck = async (req, res, next) => {
if (lastHealthCheckState) {
res.writeHead(200);
res.end('OK');
} else {
res.writeHead(500);
res.end(`NOK`);
lastHealthCheckPerformed = Date.now();
return;
}
return;
// always check if last check was unhealthy (give it another chance to recover)
}

const { lastSlotReceived, lastSlotReceivedMutex } = getSlotHealthCheckInfo();
Expand All @@ -149,7 +148,9 @@ const handleHealthCheck = async (req, res, next) => {
lastHealthCheckState = lastSlotReceived > lastHealthCheckSlot;
if (!lastHealthCheckState) {
logger.error(
`Unhealthy: lastSlot: ${lastSlotReceived}, lastHealthCheckSlot: ${lastHealthCheckSlot}`
`Unhealthy: lastSlot: ${lastSlotReceived}, lastHealthCheckSlot: ${lastHealthCheckSlot}, timeSinceLastCheck: ${
Date.now() - lastHealthCheckPerformed
} ms`
);
}

Expand Down
24 changes: 6 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
SlotSource,
SlotSubscriber,
UserMap,
UserStatsMap,
Wallet,
getUserStatsAccountPublicKey,
getVariant,
groupL2,
initialize,
Expand Down Expand Up @@ -231,8 +231,6 @@ const main = async () => {
lastSlotReceived = slotSource.getSlot();
}, ORDERBOOK_UPDATE_INTERVAL);

const userStatsMap = new UserStatsMap(driftClient);

logger.info(`Initializing userMap...`);
const initUserMapStart = Date.now();
const userMap = new UserMap(
Expand All @@ -243,12 +241,6 @@ const main = async () => {
await userMap.subscribe();
logger.info(`userMap initialized in ${Date.now() - initUserMapStart} ms`);

const initUserStatsMapStarts = Date.now();
await userStatsMap.sync(userMap.getUniqueAuthorities());
logger.info(
`userStatsMap initialized in ${Date.now() - initUserStatsMapStarts} ms`
);

logger.info(`Initializing DLOBSubscriber...`);
const initDlobSubscriberStart = Date.now();
const dlobSubscriber = new DLOBSubscriber({
Expand All @@ -265,11 +257,7 @@ const main = async () => {
MARKET_SUBSCRIBERS = await initializeAllMarketSubscribers(driftClient);

const handleStartup = async (_req, res, _next) => {
if (
driftClient.isSubscribed &&
userMap.size() > 0 &&
userStatsMap.size() > 0
) {
if (driftClient.isSubscribed && userMap.size() > 0) {
res.writeHead(200);
res.end('OK');
} else {
Expand Down Expand Up @@ -559,12 +547,12 @@ const main = async () => {
if (`${includeUserStats}`.toLowerCase() === 'true') {
const userAccount = side.userAccount.toBase58();
await userMap.mustGet(userAccount);
const userStats = await userStatsMap.mustGet(
userMap.getUserAuthority(userAccount)!.toBase58()
);
topMakers.add([
userAccount,
userStats.userStatsAccountPublicKey.toBase58(),
getUserStatsAccountPublicKey(
driftClient.program.programId,
userMap.getUserAuthority(userAccount)
),
]);
} else {
topMakers.add(side.userAccount.toBase58());
Expand Down

0 comments on commit 020e075

Please sign in to comment.