Retain and isolate delayed Companion results under backpressure
This commit is contained in:
1 parent
94daaf7e40
commit
2e84512ba4
20 files changed
+1798
-340
No files matched your search
@@ -819,6 +819,7 @@ int BaseChatMesh::sendAnonReq(const ContactInfo& recipient, const uint8_t* data,
|
||||
{
|
||||
uint8_t temp[MAX_PACKET_PAYLOAD];
|
||||
tag = getRTCClock()->getCurrentTimeUnique();
|
||||
if (!allowRequestTag(tag)) return MSG_SEND_FAILED;
|
||||
memcpy(temp, &tag, 4); // tag to match later (also extra blob to help make packet_hash unique)
|
||||
memcpy(&temp[4], data, len);
|
||||
|
||||
@@ -846,6 +847,7 @@ int BaseChatMesh::sendRequest(const ContactInfo& recipient, const uint8_t* req_
|
||||
{
|
||||
uint8_t temp[MAX_PACKET_PAYLOAD];
|
||||
tag = getRTCClock()->getCurrentTimeUnique();
|
||||
if (!allowRequestTag(tag)) return MSG_SEND_FAILED;
|
||||
memcpy(temp, &tag, 4); // mostly an extra blob to help make packet_hash unique
|
||||
memcpy(&temp[4], req_data, data_len);
|
||||
|
||||
@@ -871,6 +873,7 @@ int BaseChatMesh::sendRequest(const ContactInfo& recipient, uint8_t req_type, u
|
||||
{
|
||||
uint8_t temp[13];
|
||||
tag = getRTCClock()->getCurrentTimeUnique();
|
||||
if (!allowRequestTag(tag)) return MSG_SEND_FAILED;
|
||||
memcpy(temp, &tag, 4); // mostly an extra blob to help make packet_hash unique
|
||||
temp[4] = req_type;
|
||||
memset(&temp[5], 0, 4); // reserved (possibly for 'since' param)
|
||||
|
||||
@@ -180,6 +180,8 @@ protected:
|
||||
const uint8_t* data, size_t data_len) {}
|
||||
virtual uint8_t onContactRequest(const ContactInfo& contact, uint32_t sender_timestamp, const uint8_t* data, uint8_t len, uint8_t* reply) = 0;
|
||||
virtual void onContactResponse(const ContactInfo& contact, const uint8_t* data, uint8_t len) = 0;
|
||||
// Reject a recently retired reflected tag before allocating/transmitting.
|
||||
virtual bool allowRequestTag(uint32_t tag) { return true; }
|
||||
virtual void handleReturnPathRetry(const ContactInfo& contact, const uint8_t* path, uint8_t path_len);
|
||||
|
||||
virtual bool sendFloodScoped(const ContactInfo& recipient, mesh::Packet* pkt, uint32_t delay_millis=0);
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
#include "CompanionDelayedReplies.h"
|
||||
#include <string.h>
|
||||
|
||||
namespace mesh {
|
||||
|
||||
// Keep complete member initialization shared, not expanded into a large owner.
|
||||
#if defined(__GNUC__) && !defined(__clang__)
|
||||
__attribute__((noinline, noclone))
|
||||
#elif defined(__clang__)
|
||||
__attribute__((noinline))
|
||||
#endif
|
||||
CompanionDelayedReplies::CompanionDelayedReplies() noexcept {}
|
||||
|
||||
bool CompanionDelayedReplies::due(uint32_t now, uint32_t deadline) {
|
||||
return (int32_t)(now - deadline) >= 0;
|
||||
}
|
||||
|
||||
uint32_t CompanionDelayedReplies::retirementDeadline(
|
||||
uint32_t now, uint32_t radio_deadline, uint32_t floor) {
|
||||
uint32_t remaining = due(now, radio_deadline) ? 0 : radio_deadline - now;
|
||||
if (remaining + DELIVERY_GRACE_MS > floor) floor = remaining + DELIVERY_GRACE_MS;
|
||||
return now + floor;
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::reset(Reply& reply) {
|
||||
// The frame bytes need not be erased: Empty is the sole ownership authority.
|
||||
reply.route = nullptr;
|
||||
reply.kind = None;
|
||||
reply.phase = Empty;
|
||||
reply.history = NO_HISTORY;
|
||||
reply.frame_len = 0;
|
||||
reply.sent_pending = false;
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::arm(Reply& reply, uint32_t timeout,
|
||||
bool flood, uint32_t now) {
|
||||
reply.timeout = timeout;
|
||||
// Wrap-safe deadlines must stay in the signed half of the millis range.
|
||||
const uint32_t maximum = 0x7FFFFFFFU - DELIVERY_GRACE_MS;
|
||||
uint32_t budget = timeout > maximum / 6 * 5 ? maximum : timeout + timeout / 5;
|
||||
reply.radio_deadline = now + budget;
|
||||
reply.flood = flood;
|
||||
reply.sent_pending = !reply.terminal;
|
||||
reply.sent_deadline = now + DELIVERY_GRACE_MS;
|
||||
reply.frame_len = 0;
|
||||
reply.phase = AwaitRadio;
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::store(Reply& reply, const uint8_t* frame,
|
||||
size_t len, uint32_t now) {
|
||||
if (reply.phase != AwaitRadio || frame == nullptr || len == 0
|
||||
|| len > sizeof(reply.frame) || due(now, reply.radio_deadline)
|
||||
|| (reply.sent_pending && due(now, reply.sent_deadline))) return false;
|
||||
memcpy(reply.frame, frame, len);
|
||||
reply.frame_len = len;
|
||||
reply.delivery_deadline = now + DELIVERY_GRACE_MS;
|
||||
if (reply.sent_pending
|
||||
&& (int32_t)(reply.sent_deadline - reply.delivery_deadline) < 0) {
|
||||
reply.delivery_deadline = reply.sent_deadline;
|
||||
}
|
||||
reply.phase = AwaitAdmission;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::service(Reply& reply,
|
||||
BaseSerialInterface* serial, uint32_t now) {
|
||||
if (reply.phase == Empty || reply.phase == Reserved) return false;
|
||||
if (reply.terminal) return due(now, reply.radio_deadline);
|
||||
if (serial == nullptr || !serial->isReplyRouteAvailable(reply.route)
|
||||
|| due(now, reply.phase == AwaitAdmission
|
||||
? reply.delivery_deadline : reply.radio_deadline)
|
||||
|| (reply.sent_pending && due(now, reply.sent_deadline))) return true;
|
||||
if (reply.sent_pending) {
|
||||
uint8_t sent[10];
|
||||
sent[0] = 6; // RESP_CODE_SENT, unchanged Companion wire format.
|
||||
sent[1] = reply.flood ? 1 : 0;
|
||||
memcpy(sent + 2, &reply.tag, 4);
|
||||
memcpy(sent + 6, &reply.timeout, 4);
|
||||
if (serial->writeFrameToRoute(reply.route, sent, sizeof(sent)) != sizeof(sent)) {
|
||||
return false;
|
||||
}
|
||||
reply.sent_pending = false;
|
||||
}
|
||||
return reply.phase == AwaitAdmission
|
||||
&& serial->writeFrameToRoute(reply.route, reply.frame, reply.frame_len)
|
||||
== reply.frame_len;
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::prune(uint32_t now) {
|
||||
for (unsigned i = 0; i < HISTORY_SIZE; ++i) {
|
||||
if (requests[i].phase == AwaitAdmission && due(now, requests[i].until)) {
|
||||
requests[i].phase = Empty;
|
||||
}
|
||||
if (traces[i].phase == AwaitAdmission && due(now, traces[i].until)) {
|
||||
traces[i].phase = Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::reserveRequest(Kind kind, const uint8_t peer[32],
|
||||
BaseSerialInterface* route, bool terminal, uint32_t now) {
|
||||
prune(now);
|
||||
if (hasRequest() || kind == None || kind == Trace || peer == nullptr
|
||||
|| (!terminal && route == nullptr)) return false;
|
||||
uint8_t free = NO_HISTORY;
|
||||
for (unsigned i = 0; i < HISTORY_SIZE; ++i) {
|
||||
const RequestGuard& held = requests[i];
|
||||
if (held.phase == Empty) {
|
||||
free = i;
|
||||
} else if (kind == Login && held.login && memcmp(held.peer, peer, 32) == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (free == NO_HISTORY) return false;
|
||||
RequestGuard& held = requests[free];
|
||||
memcpy(held.peer, peer, 32);
|
||||
held.tag = 0;
|
||||
held.login = kind == Login;
|
||||
held.phase = Reserved;
|
||||
request.route = route;
|
||||
request.kind = kind;
|
||||
request.history = free;
|
||||
request.terminal = terminal;
|
||||
request.radio_deadline = now;
|
||||
request.phase = Reserved;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::allowRequestTag(uint32_t tag) {
|
||||
if (request.phase != Reserved || request.history >= HISTORY_SIZE) return false;
|
||||
RequestGuard& held = requests[request.history];
|
||||
for (unsigned i = 0; i < HISTORY_SIZE; ++i) {
|
||||
if (i != request.history && requests[i].phase != Empty
|
||||
&& requests[i].tag == tag && memcmp(requests[i].peer, held.peer, 32) == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
held.tag = tag;
|
||||
return true;
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::rememberLoginTag(uint32_t tag) {
|
||||
if (request.kind == Login && request.history < HISTORY_SIZE) {
|
||||
requests[request.history].tag = tag;
|
||||
}
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::armRequest(uint32_t tag, uint32_t timeout,
|
||||
bool flood, uint32_t now) {
|
||||
request.tag = tag;
|
||||
if (request.kind != Login && request.history < HISTORY_SIZE) {
|
||||
requests[request.history].tag = tag;
|
||||
}
|
||||
arm(request, timeout, flood, now);
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::abandonRequest() {
|
||||
if (request.history < HISTORY_SIZE) requests[request.history].phase = Empty;
|
||||
reset(request);
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::retireRequest(uint32_t now) {
|
||||
if (request.phase == Reserved) {
|
||||
abandonRequest();
|
||||
return;
|
||||
}
|
||||
if (request.history < HISTORY_SIZE && requests[request.history].phase == Reserved) {
|
||||
RequestGuard& held = requests[request.history];
|
||||
held.until = retirementDeadline(now, request.radio_deadline,
|
||||
held.login ? LOGIN_QUARANTINE_MS : DELIVERY_GRACE_MS);
|
||||
held.phase = AwaitAdmission;
|
||||
}
|
||||
reset(request);
|
||||
}
|
||||
|
||||
const uint8_t* CompanionDelayedReplies::requestPeer() const {
|
||||
return request.history < HISTORY_SIZE ? requests[request.history].peer : nullptr;
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::requestMatches(const uint8_t peer[32], uint32_t tag) const {
|
||||
const uint8_t* expected = requestPeer();
|
||||
return request.phase >= AwaitRadio && expected != nullptr
|
||||
&& memcmp(expected, peer, 32) == 0
|
||||
&& (request.kind == Login || request.tag == tag);
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::blocksResponse(const uint8_t peer[32],
|
||||
uint32_t tag, uint32_t now) const {
|
||||
// A tag-only match from another identity must not become a generic host reply.
|
||||
if (request.phase >= AwaitRadio && request.kind != Login && request.tag == tag
|
||||
&& (!requestMatches(peer, tag) || request.phase == AwaitAdmission)) return true;
|
||||
for (unsigned i = 0; i < HISTORY_SIZE; ++i) {
|
||||
const RequestGuard& held = requests[i];
|
||||
if (held.phase == Empty || memcmp(held.peer, peer, 32) != 0) continue;
|
||||
if (held.phase == AwaitAdmission && due(now, held.until)) continue;
|
||||
if (i == request.history && request.phase == AwaitRadio) continue;
|
||||
if (held.login || held.tag == tag) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::storeRequest(const uint8_t* frame, size_t len, uint32_t now) {
|
||||
return store(request, frame, len, now);
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::serviceRequest(BaseSerialInterface* serial, uint32_t now) {
|
||||
prune(now);
|
||||
if (service(request, serial, now)) retireRequest(now);
|
||||
}
|
||||
|
||||
uint8_t CompanionDelayedReplies::reserveTrace(uint32_t tag, uint32_t auth, uint32_t now) {
|
||||
prune(now);
|
||||
uint8_t free = NO_HISTORY;
|
||||
for (unsigned i = 0; i < HISTORY_SIZE; ++i) {
|
||||
const TraceGuard& held = traces[i];
|
||||
if (held.phase == Empty) {
|
||||
free = i;
|
||||
} else if (held.tag == tag && held.auth == auth) {
|
||||
return NO_HISTORY;
|
||||
}
|
||||
}
|
||||
if (free != NO_HISTORY) {
|
||||
traces[free].tag = tag;
|
||||
traces[free].auth = auth;
|
||||
traces[free].phase = Reserved;
|
||||
}
|
||||
return free;
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::abandonTrace(uint8_t history) {
|
||||
if (history < HISTORY_SIZE) traces[history].phase = Empty;
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::retireTrace(uint8_t history, uint32_t now,
|
||||
uint32_t radio_deadline) {
|
||||
if (history < HISTORY_SIZE && traces[history].phase == Reserved) {
|
||||
traces[history].until = retirementDeadline(now, radio_deadline, DELIVERY_GRACE_MS);
|
||||
traces[history].phase = AwaitAdmission;
|
||||
}
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::reserveBinaryTrace(uint32_t tag, uint32_t auth,
|
||||
BaseSerialInterface* route, uint32_t now) {
|
||||
if (hasBinaryTrace() || route == nullptr) return false;
|
||||
uint8_t history = reserveTrace(tag, auth, now);
|
||||
if (history == NO_HISTORY) return false;
|
||||
trace.route = route;
|
||||
trace.tag = tag;
|
||||
trace.auth = auth;
|
||||
trace.kind = Trace;
|
||||
trace.history = history;
|
||||
trace.terminal = false;
|
||||
trace.phase = Reserved;
|
||||
return true;
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::armBinaryTrace(uint32_t timeout, uint32_t now) {
|
||||
arm(trace, timeout, false, now);
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::abandonBinaryTrace() {
|
||||
abandonTrace(trace.history);
|
||||
reset(trace);
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::retireBinaryTrace(uint32_t now) {
|
||||
if (trace.phase == Reserved) {
|
||||
abandonBinaryTrace();
|
||||
return;
|
||||
}
|
||||
retireTrace(trace.history, now, trace.radio_deadline);
|
||||
reset(trace);
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::blocksTrace(uint32_t tag, uint32_t auth, uint32_t now) const {
|
||||
for (unsigned i = 0; i < HISTORY_SIZE; ++i) {
|
||||
if (traces[i].phase == AwaitAdmission && !due(now, traces[i].until)
|
||||
&& traces[i].tag == tag && traces[i].auth == auth) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::storeBinaryTrace(const uint8_t* frame, size_t len, uint32_t now) {
|
||||
return store(trace, frame, len, now);
|
||||
}
|
||||
|
||||
void CompanionDelayedReplies::serviceBinaryTrace(BaseSerialInterface* serial, uint32_t now) {
|
||||
prune(now);
|
||||
if (service(trace, serial, now)) retireBinaryTrace(now);
|
||||
}
|
||||
|
||||
bool CompanionDelayedReplies::hasReplyForRoute(BaseSerialInterface* route, uint32_t now) const {
|
||||
const Reply* slots[] = { &request, &trace };
|
||||
for (const Reply* reply : slots) {
|
||||
if (reply->phase >= AwaitRadio && reply->route == route
|
||||
&& !due(now, reply->phase == AwaitAdmission
|
||||
? reply->delivery_deadline : reply->radio_deadline)
|
||||
&& !(reply->sent_pending && due(now, reply->sent_deadline))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace mesh
|
||||
@@ -0,0 +1,93 @@
|
||||
#pragma once
|
||||
|
||||
#include "BaseSerialInterface.h"
|
||||
#include <stdint.h>
|
||||
|
||||
namespace mesh {
|
||||
|
||||
// Fixed producer storage, independent of the transport's bounded frame queue.
|
||||
// History is volatile and finite: it does not authenticate a host process or
|
||||
// disambiguate arbitrarily late login replies, which have no reflected tag.
|
||||
class CompanionDelayedReplies {
|
||||
public:
|
||||
CompanionDelayedReplies() noexcept;
|
||||
enum Kind : uint8_t { None, Login, Status, Telemetry, Discovery, Binary, Trace };
|
||||
enum Phase : uint8_t { Empty, Reserved, AwaitRadio, AwaitAdmission };
|
||||
static constexpr uint8_t HISTORY_SIZE = 8;
|
||||
static constexpr uint8_t NO_HISTORY = 255;
|
||||
static constexpr uint32_t DELIVERY_GRACE_MS = 10000;
|
||||
static constexpr uint32_t LOGIN_QUARANTINE_MS = 30000;
|
||||
|
||||
struct Reply {
|
||||
BaseSerialInterface* route = nullptr;
|
||||
uint32_t tag = 0, auth = 0;
|
||||
uint32_t timeout = 0, radio_deadline = 0;
|
||||
uint32_t sent_deadline = 0, delivery_deadline = 0;
|
||||
uint16_t frame_len = 0;
|
||||
Kind kind = None;
|
||||
Phase phase = Empty;
|
||||
uint8_t history = NO_HISTORY;
|
||||
bool terminal = false, sent_pending = false, flood = false;
|
||||
uint8_t frame[MAX_FRAME_SIZE] = {};
|
||||
};
|
||||
|
||||
Reply request, trace;
|
||||
|
||||
private:
|
||||
struct RequestGuard {
|
||||
uint8_t peer[32] = {};
|
||||
uint32_t tag = 0, until = 0;
|
||||
Phase phase = Empty;
|
||||
bool login = false;
|
||||
};
|
||||
struct TraceGuard {
|
||||
uint32_t tag = 0, auth = 0, until = 0;
|
||||
Phase phase = Empty;
|
||||
};
|
||||
RequestGuard requests[HISTORY_SIZE] = {};
|
||||
TraceGuard traces[HISTORY_SIZE] = {};
|
||||
|
||||
static bool due(uint32_t now, uint32_t deadline);
|
||||
static uint32_t retirementDeadline(uint32_t now, uint32_t radio_deadline,
|
||||
uint32_t floor);
|
||||
static void reset(Reply& reply);
|
||||
static void arm(Reply& reply, uint32_t timeout, bool flood, uint32_t now);
|
||||
static bool store(Reply& reply, const uint8_t* frame, size_t len, uint32_t now);
|
||||
static bool service(Reply& reply, BaseSerialInterface* serial, uint32_t now);
|
||||
void prune(uint32_t now);
|
||||
|
||||
public:
|
||||
|
||||
bool reserveRequest(Kind kind, const uint8_t peer[32],
|
||||
BaseSerialInterface* route, bool terminal, uint32_t now);
|
||||
bool allowRequestTag(uint32_t tag);
|
||||
void rememberLoginTag(uint32_t tag);
|
||||
void armRequest(uint32_t tag, uint32_t timeout, bool flood, uint32_t now);
|
||||
void abandonRequest();
|
||||
void retireRequest(uint32_t now);
|
||||
const uint8_t* requestPeer() const;
|
||||
bool requestMatches(const uint8_t peer[32], uint32_t tag) const;
|
||||
bool blocksResponse(const uint8_t peer[32], uint32_t tag, uint32_t now) const;
|
||||
bool storeRequest(const uint8_t* frame, size_t len, uint32_t now);
|
||||
void serviceRequest(BaseSerialInterface* serial, uint32_t now);
|
||||
|
||||
// Terminal TRACE reserves the same tuple history without taking the Binary
|
||||
// slot. A live reservation is never evicted to make room for another host.
|
||||
uint8_t reserveTrace(uint32_t tag, uint32_t auth, uint32_t now);
|
||||
void abandonTrace(uint8_t history);
|
||||
void retireTrace(uint8_t history, uint32_t now, uint32_t radio_deadline);
|
||||
bool reserveBinaryTrace(uint32_t tag, uint32_t auth,
|
||||
BaseSerialInterface* route, uint32_t now);
|
||||
void armBinaryTrace(uint32_t timeout, uint32_t now);
|
||||
void abandonBinaryTrace();
|
||||
void retireBinaryTrace(uint32_t now);
|
||||
bool blocksTrace(uint32_t tag, uint32_t auth, uint32_t now) const;
|
||||
bool storeBinaryTrace(const uint8_t* frame, size_t len, uint32_t now);
|
||||
void serviceBinaryTrace(BaseSerialInterface* serial, uint32_t now);
|
||||
|
||||
bool hasRequest() const { return request.phase != Empty; }
|
||||
bool hasBinaryTrace() const { return trace.phase != Empty; }
|
||||
bool hasReplyForRoute(BaseSerialInterface* route, uint32_t now) const;
|
||||
};
|
||||
|
||||
} // namespace mesh
|
||||
Reference in new issue
Block a user