Skip to content

Commit

Permalink
Reapply "Improve fastapi perfs (antonputra#391)"
Browse files Browse the repository at this point in the history
This reverts commit 19745b8.
  • Loading branch information
NewSouthMjos committed Dec 20, 2024
1 parent b6676b9 commit f677751
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lessons/232/python-app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import orjson
from asyncpg import PostgresError
from fastapi import FastAPI, HTTPException
from fastapi.responses import ORJSONResponse, PlainTextResponse
from fastapi.responses import ORJSONResponse, PlainTextResponse, Response
from prometheus_client import make_asgi_app
from pydantic import BaseModel

Expand All @@ -25,12 +25,12 @@


@app.get("/healthz", response_class=PlainTextResponse)
def health():
return "OK"
async def health():
return PlainTextResponse("OK")


@app.get("/api/devices", response_class=ORJSONResponse)
def get_devices():
async def get_devices():
devices = (
{
"id": 1,
Expand Down Expand Up @@ -58,15 +58,15 @@ def get_devices():
},
)

return devices
return ORJSONResponse(devices)


class DeviceRequest(BaseModel):
mac: str
firmware: str


@app.post("/api/devices", status_code=201, response_class=ORJSONResponse)
@app.post("/api/devices", status_code=201, response_class=Response)
async def create_device(
device: DeviceRequest, conn: PostgresDep, cache_client: MemcachedDep
):
Expand Down Expand Up @@ -102,18 +102,20 @@ async def create_device(
"updated_at": now,
}

device_json = orjson.dumps(device_dict)

# Measure cache operation
start_time = time.perf_counter()

await cache_client.set(
device_uuid.hex.encode(),
orjson.dumps(device_dict),
device_json,
exptime=20,
)

H.labels(op="set", db="memcache").observe(time.perf_counter() - start_time)

return device_dict
return Response(device_json, media_type="application/json")

except PostgresError:
logger.exception("Postgres error")
Expand Down Expand Up @@ -142,14 +144,14 @@ async def get_device_stats(cache_client: MemcachedDep):
stats = await cache_client.stats()
# H.labels(op="stats", db="memcache").observe(time.perf_counter() - start_time)

return {
return ORJSONResponse({
"curr_items": stats.get(b"curr_items", 0),
"total_items": stats.get(b"total_items", 0),
"bytes": stats.get(b"bytes", 0),
"curr_connections": stats.get(b"curr_connections", 0),
"get_hits": stats.get(b"get_hits", 0),
"get_misses": stats.get(b"get_misses", 0),
}
})
except aiomcache.exceptions.ClientException:
logger.exception("Memcached error")
raise HTTPException(
Expand Down

0 comments on commit f677751

Please sign in to comment.