Skip to content

Commit

Permalink
cache nomadnet downloader links so a new link is not established for …
Browse files Browse the repository at this point in the history
…every request to the same destination
  • Loading branch information
liamcottle committed Dec 10, 2024
1 parent 2157e3e commit bd3c6fd
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion meshchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2735,7 +2735,8 @@ def set(self, value: int):
lxmf_preferred_propagation_node_last_synced_at = IntConfig("lxmf_preferred_propagation_node_last_synced_at", None)
lxmf_local_propagation_node_enabled = BoolConfig("lxmf_local_propagation_node_enabled", False)


# FIXME: we should probably set this as an instance variable of ReticulumMeshChat so it has a proper home, and pass it in to the constructor?
nomadnet_cached_links = {}
class NomadnetDownloader:

def __init__(self, destination_hash: bytes, path: str, data: str|None, on_download_success: Callable[[bytes], None], on_download_failure: Callable[[str], None], on_progress_update: Callable[[float], None], timeout: int|None = None):
Expand All @@ -2752,6 +2753,14 @@ def __init__(self, destination_hash: bytes, path: str, data: str|None, on_downlo
# setup link to destination and request download
async def download(self, path_lookup_timeout: int = 15, link_establishment_timeout: int = 15):

# use existing established link if it's active
if self.destination_hash in nomadnet_cached_links:
link = nomadnet_cached_links[self.destination_hash]
if link.status is RNS.Link.ACTIVE:
print("[NomadnetDownloader] using existing link for request")
self.link_established(link)
return

# determine when to timeout
timeout_after_seconds = time.time() + path_lookup_timeout

Expand Down Expand Up @@ -2781,6 +2790,7 @@ async def download(self, path_lookup_timeout: int = 15, link_establishment_timeo
)

# create link to destination
print("[NomadnetDownloader] establishing new link for request")
link = RNS.Link(destination, established_callback=self.link_established)

# determine when to timeout
Expand All @@ -2797,6 +2807,9 @@ async def download(self, path_lookup_timeout: int = 15, link_establishment_timeo
# link to destination was established, we should now request the download
def link_established(self, link):

# cache link for using in future requests
nomadnet_cached_links[self.destination_hash] = link

# request download over link
link.request(
self.path,
Expand Down

0 comments on commit bd3c6fd

Please sign in to comment.