Skip to content

Commit 792a870

Browse files
feat: add semaphore
1 parent 585dd88 commit 792a870

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/backend/app/routes/ws/reverse.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
S3_CHUNK_SIZE: Final[int] = ByteSize(kb=256).total_bytes() # 256 KB read chunks
2121

22+
s3_semaphore = asyncio.Semaphore(settings.MAX_CONCURRENT_S3_READS)
23+
2224

2325
async def _stream_file_to_ws(
2426
ws: WebSocket,
@@ -122,7 +124,8 @@ async def room_ws(
122124
done_event = asyncio.Event()
123125

124126
async def _dispatch_file(entry: RoomFileEntry) -> None:
125-
await _stream_file_to_ws(ws, entry, s3_client, send_lock)
127+
async with s3_semaphore:
128+
await _stream_file_to_ws(ws, entry, s3_client, send_lock)
126129

127130
async def _listen_and_stream() -> None:
128131
seen_keys: set[str] = {e.key for e in room.files}

src/backend/app/settings.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn | str:
7474

7575
STATE_REDIS_KEY: str = "chithi:global_state"
7676
STATE_CHANNEL: str = "chithi:state_changed"
77-
# Upload cleanup: stale in-flight uploads older than this (seconds) are cleared
78-
UPLOAD_STALE_SECONDS: int = 600
77+
78+
# Instance Limits for S3 ops
79+
80+
MAX_CONCURRENT_S3_READS = 10
7981

8082

8183
settings = Settings() # type: ignore

0 commit comments

Comments
 (0)