Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle concu #241

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
],
"dependencies": {
"@myunisoft/redis": "github:MyUnisoft/redis#v3.4.2",
"@openally/mutex": "^1.0.0",
"ajv": "^8.12.0",
"pino-pretty": "^10.3.1",
"ts-pattern": "^4.3.0"
Expand Down
31 changes: 22 additions & 9 deletions src/class/eventManagement/dispatcher/transaction-handler.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-lines */
// Import Third-party Dependencies
import { Channel } from "@myunisoft/redis";
import { Mutex } from "@openally/mutex";

// Import Internal Dependencies
import {
Expand Down Expand Up @@ -88,6 +89,8 @@ export class TransactionHandler<T extends GenericEvent = GenericEvent> {
private logger: PartialLogger;
private standardLogFn: StandardLog<T>;

private resolveTransactionsLock = new Mutex({ concurrency: 1 });

constructor(opts: TransactionHandlerOptions<T>) {
Object.assign(this, opts);

Expand All @@ -96,10 +99,17 @@ export class TransactionHandler<T extends GenericEvent = GenericEvent> {
}

public async resolveTransactions() {
await this.handleBackupIncomerTransactions();
const free = await this.resolveTransactionsLock.acquire();

try {
await this.handleBackupIncomerTransactions();

await this.resolveSpreadTransactions();
await this.resolveMainTransactions();
await this.resolveSpreadTransactions();
await this.resolveMainTransactions();
}
finally {
free();
}
}

public async resolveInactiveIncomerTransactions(
Expand Down Expand Up @@ -420,6 +430,7 @@ export class TransactionHandler<T extends GenericEvent = GenericEvent> {
this.backupIncomerTransactionStore.getTransactions(),
this.dispatcherTransactionStore.getTransactions()
]);

const toResolve = [];

for (const [backupTransactionId, backupIncomerTransaction] of backupIncomerTransactions.entries()) {
Expand Down Expand Up @@ -537,14 +548,15 @@ export class TransactionHandler<T extends GenericEvent = GenericEvent> {
}

private async resolveSpreadTransactions() {
const [incomers, dispatcherTransactions, backupIncomerTransactions] = await Promise.all([
const [incomers, backupIncomerTransactions, dispatcherTransactions] = await Promise.all([
this.incomerStore.getIncomers(),
this.dispatcherTransactionStore.getTransactions(),
this.backupIncomerTransactionStore.getTransactions()
this.backupIncomerTransactionStore.getTransactions(),
this.dispatcherTransactionStore.getTransactions()
]);

const toResolve = [];
const incomerStateToUpdate = new Set<string>();

for (const [dispatcherTransactionId, dispatcherTransaction] of dispatcherTransactions.entries()) {
const transactionRecipient = dispatcherTransaction.redisMetadata.to;

Expand Down Expand Up @@ -637,14 +649,15 @@ export class TransactionHandler<T extends GenericEvent = GenericEvent> {
}

private async resolveMainTransactions() {
const [incomers, dispatcherTransactions, backupIncomerTransactions] = await Promise.all([
const [incomers, backupIncomerTransactions, dispatcherTransactions] = await Promise.all([
this.incomerStore.getIncomers(),
this.dispatcherTransactionStore.getTransactions(),
this.backupIncomerTransactionStore.getTransactions()
this.backupIncomerTransactionStore.getTransactions(),
this.dispatcherTransactionStore.getTransactions()
]);

const toResolve = [];
const incomerStateToUpdate = new Set<string>();

for (const incomer of incomers) {
const incomerStore = new TransactionStore({
prefix: `${incomer.prefix ? `${incomer.prefix}-` : ""}${incomer.providedUUID}`,
Expand Down
Loading