Skip to content

Commit

Permalink
feat: WebSocket ping functionality
Browse files Browse the repository at this point in the history
feat: dockerfile uvicorn only, gunicorn removed
  • Loading branch information
albertkun committed Nov 21, 2023
1 parent c120f79 commit 8c05735
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
6 changes: 1 addition & 5 deletions fastapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ COPY . .
# Create a new user and switch to that user
RUN useradd -ms /bin/bash pgbouncer && \
chown -R pgbouncer:pgbouncer /etc/pgbouncer

RUN pip install gevent

CMD python -c "\
import os;\
from urllib.parse import urlparse;\
Expand All @@ -48,6 +45,5 @@ print(f'export DB_NAME={result.path[1:]}')" > /tmp/env.sh && \
echo "auth_file = /etc/pgbouncer/userlist.txt" >> /etc/pgbouncer/pgbouncer.ini && \
echo "client_login_timeout = 120" >> /etc/pgbouncer/pgbouncer.ini && \
gosu pgbouncer pgbouncer /etc/pgbouncer/pgbouncer.ini & \
gunicorn app.main:app --workers 3 --worker-class gevent --bind 0.0.0.0:80 --timeout 120 --keep-alive 120

exec uvicorn app.main:app --host 0.0.0.0 --port 80 --workers 2
EXPOSE 80
51 changes: 34 additions & 17 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from datetime import timedelta, date, datetime

from fastapi import FastAPI, Request, Response, Depends, HTTPException, status, Query, Websocket
from fastapi import FastAPI, Request, Response, Depends, HTTPException, status, Query, Websocket,WebSocketDisconnect
from fastapi import Path as FastAPIPath
# from fastapi import FastAPI, Request, Response, Depends, HTTPException, status
from fastapi.encoders import jsonable_encoder
Expand Down Expand Up @@ -398,31 +398,48 @@ async def get_list_of_field_values(agency_id: AgencyIdEnum, field: VehiclePositi
raise HTTPException(status_code=404, detail="Data not found")
return data


@app.websocket("/ws/{agency_id}/vehicle_positions")
async def websocket_endpoint(websocket: WebSocket, agency_id: str, async_db: AsyncSession = Depends(get_async_db)):
await websocket.accept()
while True:
data = await crud.get_all_data_async(async_db, models.VehiclePositions, agency_id)
if data is not None:
await websocket.send_json(data)
await asyncio.sleep(5)
try:
while True:
try:
data = await asyncio.wait_for(crud.get_all_data_async(async_db, models.VehiclePositions, agency_id), timeout=120)
if data is not None:
await websocket.send_json(data)
await asyncio.sleep(10)
# Send a ping every 10 seconds
await websocket.send_json({"type": "ping"})
except asyncio.TimeoutError:
raise HTTPException(status_code=408, detail="Request timed out")
except WebSocketDisconnect:
# Handle the WebSocket disconnect event
print("WebSocket disconnected")


@app.websocket("/ws/{agency_id}/vehicle_positions/{field}/{ids}")
async def websocket_vehicle_positions_by_ids(websocket: WebSocket, agency_id: AgencyIdEnum, field: VehiclePositionsFieldsEnum, ids: str, async_db: AsyncSession = Depends(get_async_db)):
await websocket.accept()
model = models.VehiclePositions
ids = ids.split(',')
while True:
data = {}
for id in ids:
result = await crud.get_data_async(async_db, model, agency_id.value, field.value, id)
if result is not None:
data[id] = result
if data:
await websocket.send_json(data)
await asyncio.sleep(5)

try:
while True:
data = {}
for id in ids:
try:
result = await asyncio.wait_for(crud.get_data_async(async_db, model, agency_id.value, field.value, id), timeout=120)
if result is not None:
data[id] = result
except asyncio.TimeoutError:
raise HTTPException(status_code=408, detail="Request timed out")
if data:
await websocket.send_json(data)
await asyncio.sleep(5)
# Send a ping every 5 seconds
await websocket.send_json({"type": "ping"})
except WebSocketDisconnect:
# Handle the WebSocket disconnect event
print("WebSocket disconnected")

##### todo: Needs to be tested

Expand Down

0 comments on commit 8c05735

Please sign in to comment.