Repository navigation
Expand file tree
/
Copy pathhttp_service.py
More file actions
175 lines (130 loc) · 6.47 KB
/
Copy pathhttp_service.py
File metadata and controls
175 lines (130 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""A real HTTP service: FastAPI entrypoint, health probes, graceful drain.
This example shows how to:
1. Run a `FastApiEntrypoint` under the Host. The entrypoint carries its own
`HttpConfig` (never global settings), builds the app during `bind`, and runs
uvicorn with uvicorn's own signal capture neutralized -- the Host owns
signals and the whole lifecycle.
2. Resolve per-request dependencies from the unit scope the adapter opens
around every request (`UnitScopeDep`), here backed by a real dishka
container: `Scope.APP` <-> app scope, `Scope.REQUEST` <-> unit scope.
3. Get `/system/health/livez` and `/system/health/readyz` for free, driven by
the very same `HealthRegistry` the Host flips, and see a `ServiceError`
render as an RFC 9457 `application/problem+json` document on the wire.
4. Drain gracefully: `serve()` returns while the server is still accepting, so
readiness flips to false BEFORE `drain()` closes the listener -- a load
balancer removes the pod while in-flight requests still finish.
The interleaved `Request started` / `Request finished` lines below come from the
adapter's `LoggingMiddleware`, which logs through the configured logging sink and
carries the `request_id` the context middleware bound (the same id the response
returns in `X-Request-ID`).
Run with: `python examples/http_service.py`. It starts a real uvicorn server on
an ephemeral loopback port, drives it with an httpx client, shuts down
gracefully and exits 0.
"""
from __future__ import annotations
import asyncio
import json
from dataclasses import dataclass
from typing import Any
import httpx
from dishka import Provider, Scope, make_async_container, provide
from fastapi import APIRouter
from servicewright import AppSpec, ErrorKind, Service, ServiceError, UnitScopeProtocol
from servicewright.adapters.dishka import DishkaContainer
from servicewright.adapters.fastapi import FastApiEntrypoint, HttpConfig, UnitScopeDep
SERVICE_NAME = "orders-service"
GRACEFUL_TIMEOUT_SECONDS = 2.0
STARTUP_POLL_ATTEMPTS = 200
STARTUP_POLL_INTERVAL = 0.01
@dataclass(frozen=True)
class Settings:
"""All `BaseServiceSettingsProtocol` asks for; every observability section is off."""
logging: object | None = None
metrics: object | None = None
tracing: object | None = None
error_tracking: object | None = None
def get_app_version(self) -> str:
return "1.0.0"
class OrderMissingError(ServiceError):
"""Business error; the code auto-derives to `order_missing`."""
kind = ErrorKind.NOT_FOUND
class OrderRepository:
"""An APP-scoped singleton -- one instance for the whole process."""
def __init__(self) -> None:
self._orders = {"42": {"id": "42", "item": "anvil", "status": "shipped"}}
def get(self, order_id: str) -> dict[str, str]:
order = self._orders.get(order_id)
if order is None:
raise OrderMissingError("No such order.", params={"order_id": order_id})
return order
class AppProvider(Provider):
"""The dishka wiring: one APP-scoped repository."""
@provide(scope=Scope.APP)
def order_repository(self) -> OrderRepository:
return OrderRepository()
router = APIRouter()
async def load_order(scope: UnitScopeProtocol, order_id: str) -> dict[str, str]:
"""Transport-free business code: all it needs is a scope to resolve from."""
repository = await scope.get(OrderRepository)
return repository.get(order_id)
@router.get("/orders/{order_id}")
async def get_order(order_id: str, scope: UnitScopeDep) -> dict[str, str]:
"""The route: hand the per-request unit scope to the business code."""
return await load_order(scope, order_id)
def build_container(_settings: Settings) -> DishkaContainer:
"""The AppSpec's container factory: a dishka container behind the adapter."""
return DishkaContainer(make_async_container(AppProvider()))
async def wait_until_serving(client: httpx.AsyncClient, path: str) -> None:
"""Poll the readiness route until the socket actually accepts connections."""
for _ in range(STARTUP_POLL_ATTEMPTS):
try:
await client.get(path)
except httpx.TransportError:
await asyncio.sleep(STARTUP_POLL_INTERVAL)
continue
return
raise RuntimeError("server did not start in time")
async def call_service(base_url: str, ready_flag: str) -> None:
"""Drive the running server over a real HTTP client."""
async with httpx.AsyncClient(base_url=base_url, timeout=5.0) as client:
await wait_until_serving(client, "/system/health/readyz")
print(f"\n--- calling the service on {base_url} (readiness flag={ready_flag}) ---")
for path in ("/orders/42", "/orders/999", "/system/health/livez", "/system/health/readyz"):
response = await client.get(path)
content_type = response.headers.get("content-type", "")
print(f"[client] GET {path:<26} -> {response.status_code} {content_type}")
print(f"[client] {json.dumps(response.json(), sort_keys=True)}")
async def main() -> None:
spec: AppSpec[Settings, DishkaContainer] = AppSpec(
service_name=SERVICE_NAME,
create_container=build_container,
)
http = FastApiEntrypoint(
config=HttpConfig(
host="127.0.0.1",
# 0 = let the OS pick; the entrypoint reports it as `bound_port`
# once `bind()` has opened the socket.
port=0,
title=SERVICE_NAME,
graceful_timeout=GRACEFUL_TIMEOUT_SECONDS,
),
routers=(router,),
)
service = Service(spec, entrypoints=[http])
# Supplying `stop` is the embedding path: the Host installs no signal
# handlers. A deployed service omits it and lets SIGTERM set this event.
stop = asyncio.Event()
async def drive_then_stop() -> None:
while not spec.health.ready:
await asyncio.sleep(STARTUP_POLL_INTERVAL)
await call_service(f"http://127.0.0.1:{http.bound_port}", str(spec.health.ready))
print("\n--- graceful shutdown ---")
stop.set()
print(f"[server] starting {SERVICE_NAME} on 127.0.0.1 (ephemeral port)")
await asyncio.gather(service.run(Settings(), stop=stop), drive_then_stop())
print(f"[server] uvicorn drained; readiness flag is now {spec.health.ready}")
# The built app outlives the run, which is what makes `build_app` testable.
app: Any = http.app
print(f"[server] the app built at bind() is still inspectable: title={app.title!r}")
if __name__ == "__main__":
asyncio.run(main())