message: indexer-service: Less payment flushing with more predictable latency
diff: @@ -133,7 +133,7 @@ export class ReceiptManager {
  timer(30_000).pipe(async () => {
  try {
  - while (await this._flushOne());
  + await this._flushOutstanding()
  } catch (err) {
  logger.error(
  `Failed to sync payment to the db. If this does not correct itself, revenue may be lost.`,
  @@ -186,23 +186,20 @@ export class ReceiptManager {
  // * This receipt ID is not being "forked" by concurrent usage.
  }
  - private async _flushOne(): Promise<boolean> {
  - if (!this._flushQueue.length) {
  - return false
  - }
  + /// Flushes all receipts that have been registered by this moment in time.
  + private async _flushOutstanding(): Promise<void> {
  + let count = this._flushQueue.length
  - const index = Math.floor(Math.random() * this._flushQueue.length)
  - const swap = this._flushQueue[index]
  - this._flushQueue[index] = this._flushQueue[this._flushQueue.length - 1]
  - this._flushQueue[this._flushQueue.length - 1] = swap
  - // Classic swap-n-pop. The list has already been verified to be non-empty.
  - // Therefore, the ! operator is being used as intended. The overzealous
  - // arbiters of code that maintain eslint can step aside.
  - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  - const qualifiedId = this._flushQueue.pop()!
  + while (count > 0) {
  + count -= 1
  +
  + // Swap and pop
  + const qualifiedId = this._flushQueue[count]
  + this._flushQueue[count] = this._flushQueue[this._flushQueue.length - 1]
  + this._flushQueue.pop()
  // An invariant of this class is that _flushQueue indexes
  - // _cache. So, the ! is being used as intended again.
  + // _cache. So, the ! is being used as intended.
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  const receipt = this._cache.get(qualifiedId)!
  this._cache.delete(qualifiedId)
  @@ -271,7 +268,7 @@ export class ReceiptManager {
  this._queue(receipt)
  throw err
  }
  - return true
  + }
  }
  _queue(receipt: ReceiptAttributes): void {
  