refactor: enhance TTSCache with context manager for lock handling and improve error logging
This commit is contained in:
+29
-25
@@ -21,7 +21,7 @@ from fastapi.staticfiles import StaticFiles
|
||||
import httpx
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from app.tts_cache import TTSCache
|
||||
from app.tts_cache import CacheIOError, TTSCache
|
||||
from app.knowledge import KnowledgeBase, format_knowledge_context, resolve_knowledge_dir
|
||||
|
||||
load_dotenv()
|
||||
@@ -856,28 +856,29 @@ class VoiceSession:
|
||||
"similarity_boost": settings.elevenlabs_tts_similarity_boost,
|
||||
}
|
||||
|
||||
# Generate key & fetch lock dedicated ONLY to specific text/voice config
|
||||
key = tts_cache._generate_key(**cache_params)
|
||||
lock = await tts_cache.get_lock(key)
|
||||
|
||||
async with lock:
|
||||
# Clean context manager handles acquisition, locking, and resource release automatically
|
||||
async with tts_cache.lock(key):
|
||||
# Check cache
|
||||
cached_audio = await tts_cache.get(**cache_params)
|
||||
try:
|
||||
cached_audio = await tts_cache.get(**cache_params)
|
||||
if cached_audio:
|
||||
logger.info("Using cached TTS audio for text: %s", prepared_text[:30])
|
||||
await self.send_json(
|
||||
{
|
||||
"type": "tts_audio",
|
||||
"audio": base64.b64encode(cached_audio).decode("ascii"),
|
||||
"format": audio_format_label(output_format),
|
||||
"sample_rate": audio_sample_rate(output_format),
|
||||
"mime_type": audio_mime_type(output_format),
|
||||
}
|
||||
)
|
||||
return
|
||||
except Exception as exc:
|
||||
logger.warning("Failed to read TTS cache: %s", exc)
|
||||
|
||||
if cached_audio:
|
||||
logger.info("Using cached TTS audio for text: %s", prepared_text[:30])
|
||||
await self.send_json(
|
||||
{
|
||||
"type": "tts_audio",
|
||||
"audio": base64.b64encode(cached_audio).decode("ascii"),
|
||||
"format": audio_format_label(output_format),
|
||||
"sample_rate": audio_sample_rate(output_format),
|
||||
"mime_type": audio_mime_type(output_format),
|
||||
}
|
||||
)
|
||||
return
|
||||
|
||||
# Miss: Only ONE request per unique key executes the API call
|
||||
# Miss (Executed only once across concurrent tasks for this key)
|
||||
logger.info(
|
||||
"Calling ElevenLabs HTTP TTS: model=%s voice_id=%s language=%s",
|
||||
settings.elevenlabs_tts_model,
|
||||
@@ -911,16 +912,19 @@ class VoiceSession:
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
# Put in cache
|
||||
await tts_cache.put(audio_data=response.content, **cache_params)
|
||||
# Isolated write: I/O disk failures will not disrupt returning audio to user
|
||||
try:
|
||||
await tts_cache.put(audio_data=response.content, **cache_params)
|
||||
except CacheIOError as exc:
|
||||
logger.error("Failed to write TTS audio to cache: %s", exc)
|
||||
|
||||
await self.send_json(
|
||||
{
|
||||
"type": "tts_audio",
|
||||
"audio": base64.b64encode(response.content).decode("ascii"),
|
||||
"format": audio_format_label(settings.elevenlabs_tts_output_format),
|
||||
"sample_rate": audio_sample_rate(settings.elevenlabs_tts_output_format),
|
||||
"mime_type": audio_mime_type(settings.elevenlabs_tts_output_format),
|
||||
"format": audio_format_label(output_format),
|
||||
"sample_rate": audio_sample_rate(output_format),
|
||||
"mime_type": audio_mime_type(output_format),
|
||||
}
|
||||
)
|
||||
except httpx.HTTPStatusError as exc:
|
||||
@@ -929,7 +933,7 @@ class VoiceSession:
|
||||
f"ElevenLabs HTTP TTS error: {exc.response.status_code} {message}"
|
||||
)
|
||||
except Exception as exc:
|
||||
await self.send_tts_failed(f"ElevenLabs HTTP TTS error: {exc}")
|
||||
await self.send_tts_failed(f"ElevenLabs HTTP TTS error: {exc}")
|
||||
|
||||
async def stream_tts(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user