Improved startup time for instances and programs connected to a shared instance

This commit is contained in:
Mark Qvist 2022-11-24 13:28:22 +01:00
parent 1e477c976c
commit 680d17fb98

View File

@ -144,13 +144,14 @@ class Transport:
RNS.log("Loaded Transport Identity from storage", RNS.LOG_VERBOSE) RNS.log("Loaded Transport Identity from storage", RNS.LOG_VERBOSE)
packet_hashlist_path = RNS.Reticulum.storagepath+"/packet_hashlist" packet_hashlist_path = RNS.Reticulum.storagepath+"/packet_hashlist"
if os.path.isfile(packet_hashlist_path): if not Transport.owner.is_connected_to_shared_instance:
try: if os.path.isfile(packet_hashlist_path):
file = open(packet_hashlist_path, "rb") try:
Transport.packet_hashlist = umsgpack.unpackb(file.read()) file = open(packet_hashlist_path, "rb")
file.close() Transport.packet_hashlist = umsgpack.unpackb(file.read())
except Exception as e: file.close()
RNS.log("Could not load packet hashlist from storage, the contained exception was: "+str(e), RNS.LOG_ERROR) except Exception as e:
RNS.log("Could not load packet hashlist from storage, the contained exception was: "+str(e), RNS.LOG_ERROR)
# Create transport-specific destinations # Create transport-specific destinations
Transport.path_request_destination = RNS.Destination(None, RNS.Destination.IN, RNS.Destination.PLAIN, Transport.APP_NAME, "path", "request") Transport.path_request_destination = RNS.Destination(None, RNS.Destination.IN, RNS.Destination.PLAIN, Transport.APP_NAME, "path", "request")
@ -2081,41 +2082,42 @@ class Transport:
@staticmethod @staticmethod
def save_packet_hashlist(): def save_packet_hashlist():
if hasattr(Transport, "saving_packet_hashlist"): if not Transport.owner.is_connected_to_shared_instance:
wait_interval = 0.2 if hasattr(Transport, "saving_packet_hashlist"):
wait_timeout = 5 wait_interval = 0.2
wait_start = time.time() wait_timeout = 5
while Transport.saving_packet_hashlist: wait_start = time.time()
time.sleep(wait_interval) while Transport.saving_packet_hashlist:
if time.time() > wait_start+wait_timeout: time.sleep(wait_interval)
RNS.log("Could not save packet hashlist to storage, waiting for previous save operation timed out.", RNS.LOG_ERROR) if time.time() > wait_start+wait_timeout:
return False RNS.log("Could not save packet hashlist to storage, waiting for previous save operation timed out.", RNS.LOG_ERROR)
return False
try: try:
Transport.saving_packet_hashlist = True Transport.saving_packet_hashlist = True
save_start = time.time() save_start = time.time()
if not RNS.Reticulum.transport_enabled(): if not RNS.Reticulum.transport_enabled():
Transport.packet_hashlist = [] Transport.packet_hashlist = []
else: else:
RNS.log("Saving packet hashlist to storage...", RNS.LOG_DEBUG) RNS.log("Saving packet hashlist to storage...", RNS.LOG_DEBUG)
packet_hashlist_path = RNS.Reticulum.storagepath+"/packet_hashlist" packet_hashlist_path = RNS.Reticulum.storagepath+"/packet_hashlist"
file = open(packet_hashlist_path, "wb") file = open(packet_hashlist_path, "wb")
file.write(umsgpack.packb(Transport.packet_hashlist)) file.write(umsgpack.packb(Transport.packet_hashlist))
file.close() file.close()
save_time = time.time() - save_start save_time = time.time() - save_start
if save_time < 1: if save_time < 1:
time_str = str(round(save_time*1000,2))+"ms" time_str = str(round(save_time*1000,2))+"ms"
else: else:
time_str = str(round(save_time,2))+"s" time_str = str(round(save_time,2))+"s"
RNS.log("Saved packet hashlist in "+time_str, RNS.LOG_DEBUG) RNS.log("Saved packet hashlist in "+time_str, RNS.LOG_DEBUG)
except Exception as e: except Exception as e:
RNS.log("Could not save packet hashlist to storage, the contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Could not save packet hashlist to storage, the contained exception was: "+str(e), RNS.LOG_ERROR)
Transport.saving_packet_hashlist = False Transport.saving_packet_hashlist = False
@staticmethod @staticmethod