[EBT] Explicit empty-array check to avoid HTTP requests (#135638)

This commit is contained in:
Alejandro Fernández Haro 2022-07-05 14:32:03 +02:00 committed by GitHub
parent 789e2e9ceb
commit c3371d4308
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,6 +21,7 @@ import {
tap,
delayWhen,
takeUntil,
map,
} from 'rxjs';
import type {
AnalyticsClientInitContext,
@ -227,16 +228,20 @@ export class ElasticV3ServerShipper implements IShipper {
(this.internalQueue.length > 0 &&
(this.shutdown$.isStopped || Date.now() - this.lastBatchSent >= 10 * MINUTE)) ||
(Date.now() - this.lastBatchSent >= 10 * SECOND &&
(this.internalQueue.length === 1000 ||
(this.internalQueue.length === MAX_NUMBER_OF_EVENTS_IN_INTERNAL_QUEUE ||
this.getQueueByteSize(this.internalQueue) >= 10 * KIB))
),
// Send the events
concatMap(async () => {
// Send the events:
// 1. Set lastBatchSent and retrieve the events to send (clearing the queue) in a synchronous operation to avoid race conditions.
map(() => {
this.lastBatchSent = Date.now();
const eventsToSend = this.getEventsToSend();
await this.sendEvents(eventsToSend);
})
return this.getEventsToSend();
}),
// 2. Skip empty buffers
filter((events) => events.length > 0),
// 3. Actually send the events
concatMap(async (eventsToSend) => await this.sendEvents(eventsToSend))
)
.subscribe();
}