fix: review findings (async SQS dispatcher, backoff, batch insert, shutdown order, pagination, HMAC) + loadtest fixes; v0.1.6
This commit is contained in:
+19
-9
@@ -49,13 +49,23 @@ class API:
|
||||
def _req(self, method, path, body=None):
|
||||
url = self.base + path
|
||||
data = json.dumps(body).encode() if body is not None else None
|
||||
req = urllib.request.Request(
|
||||
url, data=data, method=method,
|
||||
headers={"Authorization": "Bearer " + self.token,
|
||||
"Content-Type": "application/json"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=30, context=self.ctx) as r:
|
||||
return json.loads(r.read().decode())
|
||||
# Внешний путь платформы даёт таймауты ~31-33с (~5.5% запросов) —
|
||||
# ретраи 3 с паузами (см. doc/sqs-integration.md, тикет Nubes).
|
||||
last = None
|
||||
for attempt in range(3):
|
||||
try:
|
||||
req = urllib.request.Request(
|
||||
url, data=data, method=method,
|
||||
headers={"Authorization": "Bearer " + self.token,
|
||||
"Content-Type": "application/json"},
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=15, context=self.ctx) as r:
|
||||
raw = r.read().decode()
|
||||
return json.loads(raw) if raw.strip() else {}
|
||||
except Exception as e:
|
||||
last = e
|
||||
time.sleep(1 + attempt)
|
||||
raise last
|
||||
|
||||
def create_device(self, ns, name, device_id):
|
||||
return self._req("POST", f"/v1/namespaces/{ns}/iot/devices",
|
||||
@@ -70,8 +80,8 @@ class API:
|
||||
def delete_device(self, ns, name):
|
||||
return self._req("DELETE", f"/v1/namespaces/{ns}/iot/devices/{name}")
|
||||
|
||||
def telemetry(self, ns, device_id="", limit=1000):
|
||||
q = f"?limit={limit}"
|
||||
def telemetry(self, ns, device_id="", limit=1000, offset=0):
|
||||
q = f"?limit={limit}&offset={offset}"
|
||||
if device_id:
|
||||
q += f"&device_id={device_id}"
|
||||
return self._req("GET", f"/v1/namespaces/{ns}/iot/telemetry{q}")
|
||||
|
||||
Reference in New Issue
Block a user