chunk() never resets its size counter: past 500 KB every queued event is uploaded as its own request
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
- Ambito
- backend-api-design, performance
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di segmentio/analytics-react-native
-
bug investigate
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
segmentio/analytics-react-native#1145 ·
-
enhancement
Difficoltà 4/5 3-5 giorni Idoneità per principianti 68/100
segmentio/analytics-react-native#1328 ·
-
bug investigate
Difficoltà 3/5 1-2 giorni Idoneità per principianti 78/100
segmentio/analytics-react-native#1309 ·
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
segmentio/analytics-react-native#1124 · 3 commenti ·
-
Dont merge user traits Apertaenhancement
Difficoltà 3/5 1-2 giorni Idoneità per principianti 48/100
segmentio/analytics-react-native#1114 ·
Tutte le issue di segmentio/analytics-react-native
Issue simili
-
bug(cli): hapi doctor inline-media prints a fabricated B:\ helper-script path in packaged installs Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
Crush Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
catppuccin/catppuccin#3125 ·
-
Add a SECURITY.md Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
ElementsProject/cln-application#167 · 1 commento · 1 reazione ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
Quantco/pnpm-licenses#17 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100