Skip to content

Commit

Permalink
chore: revert acquireWriteLock function
Browse files Browse the repository at this point in the history
  • Loading branch information
hussedev committed Sep 10, 2024
1 parent bc96e92 commit 1500aff
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,51 +95,26 @@ export class Database {
async acquireWriteLock() {
const client = await this.#connectionPool.connect();

// Helper function to generate lock ID based on schema name
const generateLockId = (schemaName: string): number => {
return schemaName.split("").reduce((acc, char) => {
return acc + char.charCodeAt(0);
}, 0);
};

// Helper function to acquire a lock for a specific schema
const acquireLockForSchema = async (lockId: number) => {
// generate lock id based on schema
const lockId = this.chainDataSchemaName.split("").reduce((acc, char) => {
return acc + char.charCodeAt(0);
}, 0);

try {
const result = await client.query(
`SELECT pg_try_advisory_lock(${lockId}) as lock`
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return result.rows[0].lock === true;
};

// Helper function to release a lock for a specific schema
const releaseLockForSchema = async (lockId: number) => {
await client.query(`SELECT pg_advisory_unlock(${lockId})`);
};

// Acquire locks for all schemas
const chainDataLockId = generateLockId(this.chainDataSchemaName);

// Track acquired locks
const acquiredLocks: number[] = [];

try {
const chainDataLockAcquired = await acquireLockForSchema(chainDataLockId);
if (chainDataLockAcquired) acquiredLocks.push(chainDataLockId);

this.#logger.info(`Lock Status =>
Chain Data (${chainDataLockId}): ${chainDataLockAcquired}
`);

return {
release: async () => {
for (const lockId of acquiredLocks) {
await releaseLockForSchema(lockId);
}
client.release();
},
client,
acquiredLocks,
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (result.rows[0].lock === true) {
return {
release: async () => {
await client.query(`SELECT pg_advisory_unlock(${lockId})`);
client.release();
},
client,
};
}
} catch (error) {
this.#logger.error({ error }, "Failed to acquire write lock");
}
Expand Down

0 comments on commit 1500aff

Please sign in to comment.