Skip to content

Commit 536391f

Browse files
ochafikclaude
andcommitted
fix(say-server): use adaptive polling instead of fixed initial delay
Previous approach: Always wait 150ms before first poll Problem: Even when chunks are ready, we wait unnecessarily New approach: Adaptive polling - Start polling immediately (no initial delay) - 20ms backoff when receiving chunks (fast streaming) - Exponential backoff when no chunks: 50ms → 100ms → 150ms max - Reset backoff when chunks start flowing This reduces latency when TTS is fast while being polite when it's slow. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0064873 commit 536391f

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

examples/say-server/server.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,8 @@ def generate_sync():
922922
const app = appRef.current;
923923
if (isPollingRef.current || !app) return;
924924
isPollingRef.current = true;
925-
// Initial delay to let TTS generate first chunk (reduces unnecessary polls)
926-
await new Promise(r => setTimeout(r, 150));
925+
926+
let emptyPollCount = 0;
927927
while (queueIdRef.current) {
928928
try {
929929
const result = await app.callServerTool({ name: "poll_tts_audio", arguments: { queue_id: queueIdRef.current } });
@@ -934,7 +934,17 @@ def generate_sync():
934934
}
935935
for (const chunk of data.chunks) await scheduleAudioChunk(chunk);
936936
if (data.done) { allAudioReceivedRef.current = true; break; }
937-
await new Promise(r => setTimeout(r, data.chunks.length > 0 ? 30 : 80));
937+
938+
// Adaptive backoff: faster when streaming, slower when waiting
939+
if (data.chunks.length > 0) {
940+
emptyPollCount = 0; // Reset - we're getting chunks
941+
await new Promise(r => setTimeout(r, 20)); // Fast poll during streaming
942+
} else {
943+
emptyPollCount++;
944+
// Exponential backoff for empty polls: 50ms, 100ms, 150ms max
945+
const delay = Math.min(50 + (emptyPollCount * 50), 150);
946+
await new Promise(r => setTimeout(r, delay));
947+
}
938948
} catch (err) {
939949
console.log('[TTS] Polling error:', err);
940950
break;

0 commit comments

Comments
 (0)