npaw-plugin-nwf
Version:
NPAW's Plugin
70 lines (69 loc) • 2.44 kB
TypeScript
import NQSRequest from '../nqs/NQSRequest';
import { Service } from '../nqs/Services';
/**
* Centralized queue for sending NQSRequest items with retry handling.
* Features:
* - Retry requests are processed before fresh ones
* - Failed retry stops further retries in that cycle; remaining retry requests are deferred
* - Only retry specific services
* - Retry queue is capped and sorted by timemark
* - Automatic retry scheduling with exponential backoff (decoupled from pings)
* - Max 25 retry attempts before flushing queue
*
* Enhancement:
* - If a new request is enqueued while processing, schedule one extra processing pass
* after the current cycle finishes so that newly enqueued fresh items are not stuck.
*/
export default class RequestQueue {
private freshRequests;
private retryRequests;
private activeRequests;
private isProcessing;
private destroyed;
private pendingAfterProcessing;
private retryInterval;
private retryAttemptCount;
private readonly baseRetryDelay;
private readonly maxRetryAttempts;
private readonly fixedRetryAttempts;
private readonly retryServices;
private readonly maxRetryRequests;
private readonly onSuccess;
private readonly onFail;
constructor(params: {
retryServices: Service[];
maxRetryRequests: number;
onSuccess: (request: NQSRequest) => void;
onFail: (request: NQSRequest) => void;
});
enqueue(request: NQSRequest): void;
destroy(): void;
private process;
private isRetryable;
/**
* Schedules automatic retry processing for failed requests.
* Uses exponential backoff after 10 attempts and stops after 25 attempts.
* Decoupled from pings - works even after /stop event.
* @private
*/
private _scheduleRetryProcessing;
/**
* Calculates the delay for the next retry attempt.
* First 10 attempts: fixed 5s delay
* Attempts 11-25: exponential backoff with 2min cap
* @private
*/
private _calculateRetryDelay;
/**
* Clears the active retry interval and resets attempt counter.
* Called when queue is empty or service is destroyed.
* @private
*/
private _clearRetryInterval;
/**
* Flushes the retry queue when max attempts reached.
* Calls fail callbacks for all pending requests and clears the queue.
* @private
*/
private _flushRetryQueue;
}