This commit is contained in:
Ivan FB 2026-05-29 21:41:30 +02:00
parent f1f02302f0
commit e44daec1c4
No known key found for this signature in database
GPG Key ID: DF0C67A04C543270
3 changed files with 17 additions and 29 deletions

View File

@ -60,13 +60,11 @@ type
## messaging layer has its own richer `DeliveryState` (retries, ## messaging layer has its own richer `DeliveryState` (retries,
## propagated-vs-validated); here we only model what's needed to ## propagated-vs-validated); here we only model what's needed to
## decide when a `channelReqId` is fully accounted for. ## decide when a `channelReqId` is fully accounted for.
AwaitingRateLimit AwaitingRateLimit ## Pushed by `send`; not yet released by rate_limit_manager.
## Pushed by `send`; not yet released by rate_limit_manager.
InFlight InFlight
## Released by rate_limit_manager and handed to delivery_service; ## Released by rate_limit_manager and handed to delivery_service;
## `messagingReqId` is now set. ## `messagingReqId` is now set.
Confirmed Confirmed ## `MessageSentEvent` arrived for `messagingReqId`.
## `MessageSentEvent` arrived for `messagingReqId`.
Failed Failed
## `MessageErrorEvent` arrived for `messagingReqId`, or the local ## `MessageErrorEvent` arrived for `messagingReqId`, or the local
## delivery-task construction failed before any id was reachable. ## delivery-task construction failed before any id was reachable.
@ -164,7 +162,7 @@ proc onMessageSent(self: ReliableChannel, messagingReqId: RequestId) =
## belong to this channel simply don't match and are no-ops. ## belong to this channel simply don't match and are no-ops.
self.pendingMessagingRequests.applyItIf( self.pendingMessagingRequests.applyItIf(
it.segmentSendState == SegmentSendState.InFlight and it.segmentSendState == SegmentSendState.InFlight and
it.messagingReqId == some(messagingReqId) it.messagingReqId == some(messagingReqId)
): ):
it.segmentSendState = SegmentSendState.Confirmed it.segmentSendState = SegmentSendState.Confirmed
self.pruneCompletedChannelReqs() self.pruneCompletedChannelReqs()
@ -173,7 +171,7 @@ proc onMessageError(self: ReliableChannel, messagingReqId: RequestId) =
## Symmetric to `onMessageSent` but for `MessageErrorEvent`. ## Symmetric to `onMessageSent` but for `MessageErrorEvent`.
self.pendingMessagingRequests.applyItIf( self.pendingMessagingRequests.applyItIf(
it.segmentSendState == SegmentSendState.InFlight and it.segmentSendState == SegmentSendState.InFlight and
it.messagingReqId == some(messagingReqId) it.messagingReqId == some(messagingReqId)
): ):
it.segmentSendState = SegmentSendState.Failed it.segmentSendState = SegmentSendState.Failed
self.pruneCompletedChannelReqs() self.pruneCompletedChannelReqs()
@ -196,7 +194,9 @@ proc onReadyToSend(
## live on until every sibling of their `channelReqId` is final, ## live on until every sibling of their `channelReqId` is final,
## so we walk past those to find the next one that was awaiting for this batch. ## so we walk past those to find the next one that was awaiting for this batch.
while idx < self.pendingMessagingRequests.len and while idx < self.pendingMessagingRequests.len and
self.pendingMessagingRequests[idx].segmentSendState != SegmentSendState.AwaitingRateLimit: self.pendingMessagingRequests[idx].segmentSendState !=
SegmentSendState.AwaitingRateLimit
:
idx.inc() idx.inc()
if idx >= self.pendingMessagingRequests.len: if idx >= self.pendingMessagingRequests.len:
## rate_limit_manager emitted more messages than we have pending — ## rate_limit_manager emitted more messages than we have pending —
@ -219,9 +219,7 @@ proc onReadyToSend(
MessageErrorEvent.emit( MessageErrorEvent.emit(
self.brokerCtx, self.brokerCtx,
MessageErrorEvent( MessageErrorEvent(
requestId: channelReqId, requestId: channelReqId, messageHash: "", error: "encryption failed: " & error
messageHash: "",
error: "encryption failed: " & error,
), ),
) )
## Encryption failed *before* we could hand the segment to the ## Encryption failed *before* we could hand the segment to the
@ -260,9 +258,7 @@ proc onReadyToSend(
MessageErrorEvent.emit( MessageErrorEvent.emit(
self.brokerCtx, self.brokerCtx,
MessageErrorEvent( MessageErrorEvent(
requestId: channelReqId, requestId: channelReqId, messageHash: "", error: "waku send failed: " & error
messageHash: "",
error: "waku send failed: " & error,
), ),
) )
self.pendingMessagingRequests[idx].segmentSendState = SegmentSendState.Failed self.pendingMessagingRequests[idx].segmentSendState = SegmentSendState.Failed
@ -390,9 +386,9 @@ proc new*(
## the send state machine without touching the network. ## the send state machine without touching the network.
let resolvedSendHandler = let resolvedSendHandler =
if sendHandler.isNil(): if sendHandler.isNil():
proc(envelope: MessageEnvelope): Future[Result[RequestId, string]] {. proc(
async: (raises: [CatchableError]), gcsafe envelope: MessageEnvelope
.} = ): Future[Result[RequestId, string]] {.async: (raises: [CatchableError]), gcsafe.} =
return await waku.send(envelope) return await waku.send(envelope)
else: else:
sendHandler sendHandler
@ -445,15 +441,13 @@ proc new*(
discard MessageSentEvent.listen( discard MessageSentEvent.listen(
chn.brokerCtx, chn.brokerCtx,
proc(evt: MessageSentEvent): Future[void] {.async: (raises: []).} = proc(evt: MessageSentEvent): Future[void] {.async: (raises: []).} =
chn.onMessageSent(evt.requestId) chn.onMessageSent(evt.requestId),
,
) )
discard MessageErrorEvent.listen( discard MessageErrorEvent.listen(
chn.brokerCtx, chn.brokerCtx,
proc(evt: MessageErrorEvent): Future[void] {.async: (raises: []).} = proc(evt: MessageErrorEvent): Future[void] {.async: (raises: []).} =
chn.onMessageError(evt.requestId) chn.onMessageError(evt.requestId),
,
) )
return chn return chn

View File

@ -44,9 +44,7 @@ proc new*(
let waku = ?(await createNode(conf)) let waku = ?(await createNode(conf))
let manager = T( let manager = T(
channels: initTable[ChannelId, ReliableChannel](), channels: initTable[ChannelId, ReliableChannel](), waku: waku, brokerCtx: brokerCtx
waku: waku,
brokerCtx: brokerCtx,
) )
return ok(manager) return ok(manager)

View File

@ -175,9 +175,7 @@ suite "Reliable Channel - send state machine":
var sendCalls = 0 var sendCalls = 0
let fakeSend: SendHandler = proc( let fakeSend: SendHandler = proc(
env: MessageEnvelope env: MessageEnvelope
): Future[Result[RequestId, string]] {. ): Future[Result[RequestId, string]] {.async: (raises: [CatchableError]), gcsafe.} =
async: (raises: [CatchableError]), gcsafe
.} =
sendCalls.inc sendCalls.inc
return ok(fakeMsgReqId) return ok(fakeMsgReqId)
@ -242,9 +240,7 @@ suite "Reliable Channel - send state machine":
var msgReqIds: seq[RequestId] var msgReqIds: seq[RequestId]
let fakeSend: SendHandler = proc( let fakeSend: SendHandler = proc(
env: MessageEnvelope env: MessageEnvelope
): Future[Result[RequestId, string]] {. ): Future[Result[RequestId, string]] {.async: (raises: [CatchableError]), gcsafe.} =
async: (raises: [CatchableError]), gcsafe
.} =
let id = RequestId("fake-msg-req-" & $(msgReqIds.len + 1)) let id = RequestId("fake-msg-req-" & $(msgReqIds.len + 1))
msgReqIds.add(id) msgReqIds.add(id)
return ok(id) return ok(id)