Skip to content

Commit

Permalink
Merge pull request #777 from golemfactory/bugfix/JST-678/socket-hang-up
Browse files Browse the repository at this point in the history
fix(payment): fixed payment service termination (socket-hang-up bug)
  • Loading branch information
mgordel authored Jan 30, 2024
2 parents db59802 + ec8ab41 commit dd6f412
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/payment/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const DEFAULTS = Object.freeze({
invoiceReceiveTimeout: 1000 * 60 * 5, // 5 min
maxInvoiceEvents: 500,
maxDebitNotesEvents: 500,
invoiceFetchingInterval: 20000,
debitNotesFetchingInterval: 20000,
invoiceFetchingInterval: 5_000,
debitNotesFetchingInterval: 5_000,
unsubscribeTimeoutMs: 10_000,
debitNoteFilter: acceptAllDebitNotesFilter(),
invoiceFilter: acceptAllInvoicesFilter(),
});
Expand All @@ -26,6 +27,7 @@ export interface BasePaymentOptions {
payment?: { driver?: string; network?: string };
paymentTimeout?: number;
paymentRequestTimeout?: number;
unsubscribeTimeoutMs?: number;
logger?: Logger;
eventTarget?: EventTarget;
}
Expand Down Expand Up @@ -58,6 +60,7 @@ export class PaymentConfig extends BaseConfig {
public readonly debitNotesFetchingInterval: number;
public readonly maxInvoiceEvents: number;
public readonly maxDebitNotesEvents: number;
public readonly unsubscribeTimeoutMs: number;
public readonly debitNoteFilter: DebitNoteFilter;
public readonly invoiceFilter: InvoiceFilter;

Expand All @@ -67,6 +70,7 @@ export class PaymentConfig extends BaseConfig {
this.debitNotesFetchingInterval = options?.debitNotesFetchingInterval ?? DEFAULTS.debitNotesFetchingInterval;
this.maxInvoiceEvents = options?.maxInvoiceEvents ?? DEFAULTS.maxInvoiceEvents;
this.maxDebitNotesEvents = options?.maxDebitNotesEvents ?? DEFAULTS.maxDebitNotesEvents;
this.unsubscribeTimeoutMs = options?.unsubscribeTimeoutMs ?? DEFAULTS.unsubscribeTimeoutMs;
this.debitNoteFilter = options?.debitNotesFilter ?? DEFAULTS.debitNoteFilter;
this.invoiceFilter = options?.invoiceFilter ?? DEFAULTS.invoiceFilter;
}
Expand Down
27 changes: 23 additions & 4 deletions src/payment/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Logger, sleep, YagnaApi } from "../utils";
import { Invoice } from "./invoice";
import { DebitNote } from "./debit_note";
import { Events } from "../events";
import { GolemTimeoutError } from "../error/golem-error";

export interface PaymentOptions extends BasePaymentOptions {
invoiceFetchingInterval?: number;
Expand All @@ -12,6 +13,7 @@ export interface PaymentOptions extends BasePaymentOptions {
}

export const PAYMENT_EVENT_TYPE = "PaymentReceived";
const UNSUBSCRIBED_EVENT = "Unsubscribed";

export class Payments extends EventTarget {
private isRunning = true;
Expand All @@ -34,11 +36,27 @@ export class Payments extends EventTarget {
}

/**
* Unsubscribe demand from the market
* Unsubscribe from collecting payment events.
* An error will be thrown when the unsubscribe timeout expires.
*/
async unsubscribe() {
this.isRunning = false;
this.logger.debug(`Payments unsubscribed`);
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(
() =>
reject(
new GolemTimeoutError(
`The waiting time (${this.options.unsubscribeTimeoutMs} ms) for unsubscribe payment has been exceeded.`,
),
),
this.options.unsubscribeTimeoutMs,
);
this.addEventListener(UNSUBSCRIBED_EVENT, () => {
this.logger.debug(`Payments unsubscribed`);
clearTimeout(timeoutId);
resolve(true);
});
this.isRunning = false;
});
}

private async subscribe() {
Expand All @@ -57,7 +75,7 @@ export class Payments extends EventTarget {
{ timeout: 0 },
);
for (const event of invoiceEvents) {
if (!this.isRunning) return;
if (!this.isRunning) break;
if (event.eventType !== "InvoiceReceivedEvent") continue;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore FIXME: ya-ts-client does not provide invoiceId in the event even though it is in the API response
Expand Down Expand Up @@ -88,6 +106,7 @@ export class Payments extends EventTarget {
await sleep(2);
}
}
this.dispatchEvent(new Event(UNSUBSCRIBED_EVENT));
}

private async subscribeForDebitNotes() {
Expand Down

0 comments on commit dd6f412

Please sign in to comment.