-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.py
More file actions
351 lines (295 loc) · 12 KB
/
router.py
File metadata and controls
351 lines (295 loc) · 12 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""Request Router — maps incoming paths to fleet-agent backends.
Routes follow the pattern ``/api/{agent_name}/{endpoint}`` and are resolved
to ``http://{host}:{port}/{endpoint}``. The router supports:
* Friendly-name → host:port mapping
* Regex-based route patterns for advanced matching
* Round-robin load balancing when multiple backends exist
* Middleware chain integration
"""
from __future__ import annotations
import logging
import re
import threading
from dataclasses import dataclass, field
from typing import Any, Optional
from middleware import (
AuthMiddleware,
CORSMiddleware,
Handler,
LoggingMiddleware,
MiddlewareChain,
RateLimiter,
Request,
Response,
TimeoutMiddleware,
)
logger = logging.getLogger("fleet.gateway.router")
# ---------------------------------------------------------------------------
# Service Registry Entry
# ---------------------------------------------------------------------------
@dataclass
class ServiceInstance:
"""A single running instance of a fleet agent."""
host: str
port: int
healthy: bool = True
last_check: float = 0.0
@property
def base_url(self) -> str:
return f"http://{self.host}:{self.port}"
@dataclass
class ServiceRegistration:
"""Registry entry for one named service (may have multiple instances)."""
name: str
instances: list[ServiceInstance] = field(default_factory=list)
friendly_names: list[str] = field(default_factory=list)
metadata: dict[str, str] = field(default_factory=dict)
@property
def healthy_count(self) -> int:
return sum(1 for i in self.instances if i.healthy)
def next_instance(self) -> Optional[ServiceInstance]:
"""Round-robin selection among healthy instances."""
healthy = [i for i in self.instances if i.healthy]
if not healthy:
return None
idx = id(self) % len(healthy) # simple but deterministic
return healthy[idx]
# ---------------------------------------------------------------------------
# Route Entry
# ---------------------------------------------------------------------------
@dataclass
class Route:
"""A single route rule."""
pattern: re.Pattern
agent_name: str
strip_prefix: str = "/api"
methods: list[str] = field(default_factory=lambda: ["GET", "POST", "PUT", "DELETE"])
# ---------------------------------------------------------------------------
# Request Router
# ---------------------------------------------------------------------------
class RequestRouter:
"""Routes incoming requests to the appropriate fleet agent.
The router maintains a service registry and a route table. Incoming
requests are matched against the route table; the first match wins.
Path format: ``/api/{agent_name}/{path}`` → ``http://{host}:{port}/{path}``
"""
# Regex for /api/{agent}/{path...}
API_PATTERN = re.compile(r"^/api/([^/]+)(/.*)?$")
def __init__(self) -> None:
self._registry: dict[str, ServiceRegistration] = {}
self._routes: list[Route] = []
self._rr_counters: dict[str, int] = {} # round-robin counters
self._lock = threading.Lock()
# Middleware chain
self._middleware = MiddlewareChain()
self._timeout_mw = TimeoutMiddleware(default_timeout=30.0)
self._logging_mw = LoggingMiddleware()
self._auth_mw: Optional[AuthMiddleware] = None
self._rate_limiter: Optional[RateLimiter] = None
self._cors_mw: Optional[CORSMiddleware] = None
# ---- Service Registry ------------------------------------------------
def register(
self,
name: str,
host: str,
port: int,
friendly_names: Optional[list[str]] = None,
metadata: Optional[dict[str, str]] = None,
) -> None:
"""Register (or add an instance to) a service."""
with self._lock:
if name not in self._registry:
self._registry[name] = ServiceRegistration(
name=name,
friendly_names=friendly_names or [],
metadata=metadata or {},
)
reg = self._registry[name]
# Avoid duplicate instances
for inst in reg.instances:
if inst.host == host and inst.port == port:
return
reg.instances.append(ServiceInstance(host=host, port=port))
logger.info("registered %s → %s:%d", name, host, port)
def deregister(self, name: str, host: Optional[str] = None, port: Optional[int] = None) -> bool:
"""Remove a service or a specific instance."""
with self._lock:
if name not in self._registry:
return False
reg = self._registry[name]
if host is not None and port is not None:
reg.instances = [
i for i in reg.instances
if not (i.host == host and i.port == port)
]
if not reg.instances:
del self._registry[name]
else:
del self._registry[name]
logger.info("deregistered %s", name)
return True
def get_service(self, name: str) -> Optional[ServiceRegistration]:
return self._registry.get(name)
def list_services(self) -> dict[str, ServiceRegistration]:
with self._lock:
return dict(self._registry)
def resolve_name(self, name: str) -> Optional[str]:
"""Resolve a friendly name or direct name to the canonical service name."""
with self._lock:
# Direct match
if name in self._registry:
return name
# Friendly name lookup
for svc_name, reg in self._registry.items():
if name in reg.friendly_names:
return svc_name
return None
# ---- Route Table -----------------------------------------------------
def add_route(
self,
pattern: str,
agent_name: str,
strip_prefix: str = "/api",
methods: Optional[list[str]] = None,
) -> None:
"""Add a custom route pattern."""
compiled = re.compile(pattern)
self._routes.append(Route(
pattern=compiled,
agent_name=agent_name,
strip_prefix=strip_prefix,
methods=methods or ["GET", "POST", "PUT", "DELETE"],
))
def list_routes(self) -> list[dict[str, Any]]:
"""Return all registered routes as serialisable dicts."""
routes = []
for r in self._routes:
routes.append({
"pattern": r.pattern.pattern,
"agent": r.agent_name,
"strip_prefix": r.strip_prefix,
"methods": r.methods,
})
# Built-in API routes
for name in self._registry:
routes.append({
"pattern": f"/api/{name}/<path>",
"agent": name,
"strip_prefix": f"/api/{name}",
"methods": ["GET", "POST", "PUT", "DELETE"],
})
return routes
# ---- Routing Logic ---------------------------------------------------
def route(self, req: Request) -> Optional[tuple[str, str, str]]:
"""Resolve a request to (base_url, upstream_path, agent_name).
Returns None if no matching route is found.
"""
path = req.path
# 1. Check custom routes first
for route in self._routes:
if req.method not in route.methods:
continue
m = route.pattern.match(path)
if m:
agent_name = route.agent_name
upstream_path = path[len(route.strip_prefix):]
if not upstream_path.startswith("/"):
upstream_path = "/" + upstream_path
svc = self._resolve_service(agent_name)
if svc is None:
return None
return (svc, upstream_path, agent_name)
# 2. Default /api/{agent}/{path} routing
m = self.API_PATTERN.match(path)
if m:
agent_name = m.group(1)
upstream_path = m.group(2) or "/"
canonical = self.resolve_name(agent_name)
if canonical is None:
return None
svc_url = self._round_robin(canonical)
if svc_url is None:
return None
return (svc_url, upstream_path, canonical)
return None
def _resolve_service(self, agent_name: str) -> Optional[str]:
"""Resolve agent name to a single backend URL."""
canonical = self.resolve_name(agent_name)
if canonical is None:
return None
return self._round_robin(canonical)
def _round_robin(self, canonical_name: str) -> Optional[str]:
"""Round-robin selection among healthy instances."""
reg = self._registry.get(canonical_name)
if reg is None or not reg.instances:
return None
healthy = [i for i in reg.instances if i.healthy]
if not healthy:
# Fall back to any instance if all unhealthy
healthy = reg.instances
with self._lock:
idx = self._rr_counters.get(canonical_name, 0)
self._rr_counters[canonical_name] = (idx + 1) % len(healthy)
instance = healthy[idx]
return instance.base_url
# ---- Middleware Configuration ----------------------------------------
def enable_auth(self, secret: str = "fleet-secret-token", skip_paths: Optional[set[str]] = None) -> None:
self._auth_mw = AuthMiddleware(secret=secret, skip_paths=skip_paths)
def enable_rate_limiting(self, tokens_per_minute: int = 60, burst_size: int = 10) -> None:
self._rate_limiter = RateLimiter(tokens_per_minute=tokens_per_minute, burst_size=burst_size)
def enable_cors(
self,
allow_origins: str = "*",
allow_methods: str = "GET, POST, PUT, DELETE, OPTIONS",
allow_headers: str = "Content-Type, Authorization",
max_age: int = 86400,
) -> None:
self._cors_mw = CORSMiddleware(
allow_origins=allow_origins,
allow_methods=allow_methods,
allow_headers=allow_headers,
max_age=max_age,
)
def set_timeout(self, path_prefix: str, timeout: float) -> None:
self._timeout_mw.set_timeout(path_prefix, timeout)
def get_timeout(self, path: str) -> float:
return self._timeout_mw.get_timeout(path)
@property
def logging_middleware(self) -> LoggingMiddleware:
return self._logging_mw
@property
def rate_limiter(self) -> Optional[RateLimiter]:
return self._rate_limiter
def build_chain(self, handler: Handler) -> Handler:
"""Assemble the full middleware chain around *handler*."""
chain = MiddlewareChain()
if self._cors_mw:
chain.add(self._cors_mw)
if self._auth_mw:
chain.add(self._auth_mw)
if self._rate_limiter:
chain.add(self._rate_limiter)
chain.add(self._logging_mw)
chain.add(self._timeout_mw)
return chain.build(handler)
# ---- Health / Stats --------------------------------------------------
def health_snapshot(self) -> dict[str, Any]:
"""Return health info for all registered services."""
snapshot = {}
with self._lock:
for name, reg in self._registry.items():
instances = []
for inst in reg.instances:
instances.append({
"host": inst.host,
"port": inst.port,
"healthy": inst.healthy,
})
snapshot[name] = {
"healthy": reg.healthy_count > 0,
"healthy_instances": reg.healthy_count,
"total_instances": len(reg.instances),
"instances": instances,
"friendly_names": reg.friendly_names,
}
return snapshot