Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Improve logging in SessionLock (#133)
Browse files Browse the repository at this point in the history
some more diagnostics for this, to help resolve
https://github.com/element-hq/element-desktop/issues/1495
  • Loading branch information
richvdh authored Oct 9, 2024
1 parent df4a223 commit b79343d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/utils/SessionLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,29 @@ export const SESSION_LOCK_CONSTANTS = {
* @returns true if any instance is currently active
*/
export function checkSessionLockFree(): boolean {
const prefixedLogger = logger.getChild(`checkSessionLockFree`);

const lastPingTime = window.localStorage.getItem(SESSION_LOCK_CONSTANTS.STORAGE_ITEM_PING);
if (lastPingTime === null) {
// no other holder
prefixedLogger.info("No other session has the lock");
return true;
}

const lockHolder = window.localStorage.getItem(SESSION_LOCK_CONSTANTS.STORAGE_ITEM_OWNER);

// see if it has expired
const timeAgo = Date.now() - parseInt(lastPingTime);
return timeAgo > SESSION_LOCK_CONSTANTS.LOCK_EXPIRY_TIME_MS;

const remaining = SESSION_LOCK_CONSTANTS.LOCK_EXPIRY_TIME_MS - timeAgo;
if (remaining <= 0) {
// another session claimed the lock, but it is stale.
prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago: lock is free`);
return true;
}

prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago: lock is taken`);
return false;
}

/**
Expand All @@ -95,7 +109,7 @@ export async function getSessionLock(onNewInstance: () => Promise<void>): Promis
/** unique ID for this session */
const sessionIdentifier = uuidv4();

const prefixedLogger = logger.withPrefix(`getSessionLock[${sessionIdentifier}]`);
const prefixedLogger = logger.getChild(`getSessionLock[${sessionIdentifier}]`);

/** The ID of our regular task to service the lock.
*
Expand Down Expand Up @@ -133,7 +147,7 @@ export async function getSessionLock(onNewInstance: () => Promise<void>): Promis
return 0;
}

prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago, waiting`);
prefixedLogger.info(`Last ping (from ${lockHolder}) was ${timeAgo}ms ago, waiting ${remaining}ms`);
return remaining;
}

Expand Down

0 comments on commit b79343d

Please sign in to comment.