Skip to content

Commit

Permalink
Fix reconnects lock (#1303)
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin authored Nov 13, 2024
2 parents 7970ea9 + c418518 commit e2bc011
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions backend/routers/transcribe_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import opuslib
import webrtcvad
from fastapi import APIRouter
from fastapi import APIRouter, HTTPException
from fastapi.websockets import WebSocketDisconnect, WebSocket
from pydub import AudioSegment
from starlette.websockets import WebSocketState
Expand Down Expand Up @@ -65,6 +65,9 @@ async def _websocket_util(

print('_websocket_util', uid, language, sample_rate, codec, include_speech_profile)

if not uid or len(uid) <= 0:
raise HTTPException(status_code=400, detail="Invalid UID")

# Not when comes from the phone, and only Friend's with 1.0.4
if stt_service == STTService.soniox and language not in soniox_valid_languages:
stt_service = STTService.deepgram
Expand Down Expand Up @@ -313,6 +316,7 @@ async def transcript_consume():
nonlocal websocket_active
nonlocal segment_buffers
nonlocal transcript_ws
nonlocal pusher_connected
while websocket_active or len(segment_buffers) > 0:
await asyncio.sleep(1)
if transcript_ws and len(segment_buffers) > 0:
Expand Down Expand Up @@ -344,6 +348,7 @@ async def audio_bytes_consume():
nonlocal websocket_active
nonlocal audio_buffers
nonlocal audio_bytes_ws
nonlocal pusher_connected
while websocket_active or len(audio_buffers) > 0:
await asyncio.sleep(1)
if audio_bytes_ws and len(audio_buffers) > 0:
Expand All @@ -365,7 +370,7 @@ async def audio_bytes_consume():
async def reconnect():
nonlocal pusher_connected
nonlocal pusher_connect_lock
with pusher_connect_lock:
async with pusher_connect_lock:
if pusher_connected:
return
await connect()
Expand Down
2 changes: 1 addition & 1 deletion backend/utils/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async def connect_to_trigger_pusher(uid: str, sample_rate: int = 8000, retries:
raise
backoff_delay = calculate_backoff_with_jitter(attempt)
print(f"Waiting {backoff_delay:.0f}ms before next retry...")
asyncio.sleep(backoff_delay / 1000)
await asyncio.sleep(backoff_delay / 1000)

raise Exception(f'Could not open socket: All retry attempts failed.')

Expand Down

0 comments on commit e2bc011

Please sign in to comment.