Retry with Packet.resend() instead of Packet.send() for #8

This commit is contained in:
Aaron Heise 2023-02-19 06:45:58 -06:00
parent b41bcbbc44
commit 07cf0e3d05
No known key found for this signature in database
GPG Key ID: 6BA54088C41DE8BF
3 changed files with 29 additions and 9 deletions

View File

@ -35,6 +35,10 @@ class MessageOutletBase(ABC):
def send(self, raw: bytes) -> _TReceipt: def send(self, raw: bytes) -> _TReceipt:
raise NotImplemented() raise NotImplemented()
@abstractmethod
def resend(self, receipt: _TReceipt) -> _TReceipt:
raise NotImplemented()
@property @property
@abstractmethod @abstractmethod
def mdu(self): def mdu(self):
@ -330,6 +334,10 @@ class Messenger(contextlib.AbstractContextManager):
state = MessageState.MSGSTATE_NEW if not message.receipt else outlet.get_receipt_state(message.receipt) state = MessageState.MSGSTATE_NEW if not message.receipt else outlet.get_receipt_state(message.receipt)
if state in [MessageState.MSGSTATE_NEW, MessageState.MSGSTATE_FAILED]: if state in [MessageState.MSGSTATE_NEW, MessageState.MSGSTATE_FAILED]:
try: try:
if message.receipt:
self._log.debug(f"Resending packet for {message}")
message.receipt = outlet.resend(message.receipt)
else:
self._log.debug(f"Sending packet for {message}") self._log.debug(f"Sending packet for {message}")
message.receipt = outlet.send(message.raw) message.receipt = outlet.send(message.raw)
except Exception as ex: except Exception as ex:

View File

@ -377,10 +377,14 @@ class RNSOutlet(LSOutletBase):
def teardown(self): def teardown(self):
self.link.teardown() self.link.teardown()
def send(self, raw: bytes) -> RNS.PacketReceipt: def send(self, raw: bytes) -> RNS.Packet:
packet = RNS.Packet(self.link, raw) packet = RNS.Packet(self.link, raw)
packet.send() packet.send()
return packet.receipt return packet
def resend(self, packet: RNS.Packet) -> RNS.Packet:
packet.resend()
return packet
@property @property
def mdu(self) -> int: def mdu(self) -> int:
@ -394,8 +398,8 @@ class RNSOutlet(LSOutletBase):
def is_usuable(self): def is_usuable(self):
return True #self.link.status in [RNS.Link.ACTIVE] return True #self.link.status in [RNS.Link.ACTIVE]
def get_receipt_state(self, receipt: RNS.PacketReceipt) -> MessageState: def get_receipt_state(self, packet: RNS.Packet) -> MessageState:
status = receipt.get_status() status = packet.receipt.get_status()
if status == RNS.PacketReceipt.SENT: if status == RNS.PacketReceipt.SENT:
return protocol.MessageState.MSGSTATE_SENT return protocol.MessageState.MSGSTATE_SENT
if status == RNS.PacketReceipt.DELIVERED: if status == RNS.PacketReceipt.DELIVERED:

View File

@ -22,6 +22,8 @@ class Receipt:
def __init__(self, state: rnsh.protocol.MessageState, raw: bytes): def __init__(self, state: rnsh.protocol.MessageState, raw: bytes):
self.state = state self.state = state
self.raw = raw self.raw = raw
self.msgid = uuid.uuid4()
self.tries = 1
class MessageOutletTest(rnsh.protocol.MessageOutletBase): class MessageOutletTest(rnsh.protocol.MessageOutletBase):
@ -39,6 +41,11 @@ class MessageOutletTest(rnsh.protocol.MessageOutletBase):
self.receipts.append(receipt) self.receipts.append(receipt)
return receipt return receipt
def resend(self, receipt: Receipt) -> Receipt:
receipt.tries += 1
receipt.state = rnsh.protocol.MessageState.MSGSTATE_SENT
return receipt
def set_packet_received_callback(self, cb: Callable[[rnsh.protocol.MessageOutletBase, bytes], None]): def set_packet_received_callback(self, cb: Callable[[rnsh.protocol.MessageOutletBase, bytes], None]):
self.packet_callback = cb self.packet_callback = cb
@ -104,12 +111,13 @@ def test_send_one_retry():
receipt.state = rnsh.protocol.MessageState.MSGSTATE_FAILED receipt.state = rnsh.protocol.MessageState.MSGSTATE_FAILED
module_logger.info("set failed") module_logger.info("set failed")
time.sleep(retry_interval) time.sleep(retry_interval)
assert len(outlet.receipts) == 2 assert len(outlet.receipts) == 1
receipt = outlet.receipts[1] assert receipt == outlet.receipts[0]
assert receipt.state == rnsh.protocol.MessageState.MSGSTATE_SENT assert receipt.state == rnsh.protocol.MessageState.MSGSTATE_SENT
assert receipt.tries == 2
receipt.state = rnsh.protocol.MessageState.MSGSTATE_DELIVERED receipt.state = rnsh.protocol.MessageState.MSGSTATE_DELIVERED
time.sleep(retry_interval) time.sleep(retry_interval)
assert len(outlet.receipts) == 2 assert len(outlet.receipts) == 1
assert not message.tracked assert not message.tracked