Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

chunk() never resets its size counter: past 500 KB every queued event is uploaded as its own request

Aperta Adatta ai principianti
#1,334 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
react-native, typescript

Direzione di ricerca

Il bug si trova nella funzione chunk in packages/core/src/util.ts. Inizia leggendo la funzione e l'helper sizeOf. L'issue include un caso di riproduzione e una correzione suggerita. Testa la correzione eseguendo i test esistenti per la logica di suddivisione (chunking), o crea un piccolo test per verificare che il comportamento corretto restituisca i 20 blocchi previsti invece di migliaia. Controlla se ci sono test correlati nel repository.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

chunk() in packages/core/src/util.ts never resets rollingKBSize. Once the queue's running size reaches maxKB (MAX_PAYLOAD_SIZE_IN_KB = 500), every later event goes into its own chunk. SegmentDestination.sendEvents then uploads all chunks at once with Promise.all, so a single flush sends one HTTP request per queued event.

if (maxKB !== undefined) {
  rollingKBSize += sizeOf(item);
  if (rollingKBSize >= maxKB) {
    chunks[++currentChunk] = [item];   // rollingKBSize is never reset
    return chunks;
  }
}

Reproduction

chunk(events, 1000, 500) with 10,000 events of about 1 KB each returns 9,481 chunks: one of 520 events, then 9,480 of one event each. The expected result is 20 chunks of about 520.

Impact

It shows up when the queue grows past 500 KB, which happens when uploads keep failing (for example, a device whose DNS blocks *.segmentapis.com). The queue persists across launches. If cdn-settings.segment.com is unreachable too, there's no httpConfig, so no backoff and no maxTotalBackoffDuration pruning, and maxQueueSize isn't enforced anywhere. In production we saw one iOS session make about 9,700 upload attempts in 91 seconds, at a steady ~60 per second (URLSession's 6 connections per host).

Version: 2.24.1. Same code on master.

Suggested fix

Keep a size counter per chunk and reset it when a new chunk starts, and push chunks instead of assigning by index. That also fixes the sparse-array case in #1309.

export const chunk = <T>(array: T[], count: number, maxKB?: number): T[][] => {
  if (!array.length || !count) return [];
  const result: T[][] = [];
  let current: T[] = [];
  let currentKB = 0;
  for (const item of array) {
    const itemKB = maxKB !== undefined ? sizeOf(item) : 0;
    const isFull = current.length >= count || (maxKB !== undefined && currentKB + itemKB >= maxKB);
    if (current.length > 0 && isFull) {
      result.push(current);
      current = [];
      currentKB = 0;
    }
    current.push(item);
    currentKB += itemKB;
  }
  if (current.length > 0) result.push(current);
  return result;
};
Lingua principale
TypeScript
Stelle
383
Fork
206
Merge medio
14h 45m
PR unite (30g)
11

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di segmentio/analytics-react-native

Tutte le issue di segmentio/analytics-react-native

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.