Skip to content

Commit

Permalink
🚑️ Add hotfix for websocket blocking the server
Browse files Browse the repository at this point in the history
  • Loading branch information
pajowu committed Aug 3, 2023
1 parent bf1ef14 commit 78f9c7c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ dev = [
]

[tool.pdm.scripts]
start = "uvicorn transcribee_backend.main:app"
dev = "uvicorn transcribee_backend.main:app --reload"
start = "uvicorn transcribee_backend.main:app --ws websockets"
dev = "uvicorn transcribee_backend.main:app --reload --ws websockets"
migrate = "alembic upgrade head"
makemigrations = "alembic revision --autogenerate -m"
create_user = "scripts/create_user.py"
Expand Down
22 changes: 19 additions & 3 deletions backend/transcribee_backend/helpers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,27 @@ async def listener(self):

async def broadcast_sender(self):
statement = select(DocumentUpdate).where(DocumentUpdate.document == self._doc)
message = bytes([SyncMessageType.FULL_DOCUMENT])

# START:
# Create a message as a list of bytes is hacky and only works for sure when using uvicorn
# with the `websockets` module as the ws implementation.
# `websockets` supports fragmenting the message into multiple frames, but you need to pass
# the message as a list/iterator of bytes|str. Each item of this iterator is then sent as a
# seperate frame.
# This is a quick workaround to prevent uvicorn hanging for many seconds on larger documents
#
# The message given to `send_bytes` is passed through to the `send` function of the
# websocket connection eventually. Since it is not touched on the way, we can pass a list of
# bytes here instead of just bytes as would be allowed by the asgi spec:
# https://asgi.readthedocs.io/en/latest/specs/www.html#send-send-event
message = [bytes([SyncMessageType.FULL_DOCUMENT])]
for update in self._session.exec(statement):
message += update.change_bytes
await self._ws.send_bytes(message)
message.append(update.change_bytes)
await self._ws.send_bytes(message) # type: ignore
# END

await self._ws.send_bytes(bytes([SyncMessageType.CHANGE_BACKLOG_COMPLETE]))

while True:
msg = await self._msg_queue.get()
await self._ws.send_bytes(bytes([SyncMessageType.CHANGE]) + msg)
Expand Down

0 comments on commit 78f9c7c

Please sign in to comment.