Added periodic data persistence for shared and standalone instances

This commit is contained in:
Mark Qvist 2022-09-13 20:17:25 +02:00
parent 9775893840
commit 75e32af1c5
3 changed files with 43 additions and 22 deletions

View File

@ -139,7 +139,7 @@ class Identity:
if not destination_hash in Identity.known_destinations: if not destination_hash in Identity.known_destinations:
Identity.known_destinations[destination_hash] = storage_known_destinations[destination_hash] Identity.known_destinations[destination_hash] = storage_known_destinations[destination_hash]
RNS.log("Saving known destinations to storage...", RNS.LOG_VERBOSE) RNS.log("Saving "+str(len(Identity.known_destinations))+" known destinations to storage...", RNS.LOG_VERBOSE)
file = open(RNS.Reticulum.storagepath+"/known_destinations","wb") file = open(RNS.Reticulum.storagepath+"/known_destinations","wb")
umsgpack.dump(Identity.known_destinations, file) umsgpack.dump(Identity.known_destinations, file)
file.close() file.close()
@ -247,9 +247,14 @@ class Identity:
return False return False
@staticmethod @staticmethod
def exit_handler(): def persist_data():
if not RNS.Transport.owner.is_connected_to_shared_instance:
Identity.save_known_destinations() Identity.save_known_destinations()
@staticmethod
def exit_handler():
Identity.persist_data()
@staticmethod @staticmethod
def from_bytes(prv_bytes): def from_bytes(prv_bytes):

View File

@ -127,7 +127,9 @@ class Reticulum:
MDU = MTU - HEADER_MAXSIZE - IFAC_MIN_SIZE MDU = MTU - HEADER_MAXSIZE - IFAC_MIN_SIZE
RESOURCE_CACHE = 24*60*60 RESOURCE_CACHE = 24*60*60
JOB_INTERVAL = 15*60 JOB_INTERVAL = 5*60
CLEAN_INTERVAL = 15*60
PERSIST_INTERVAL = 60*60*12
router = None router = None
config = None config = None
@ -206,6 +208,8 @@ class Reticulum:
self.is_connected_to_shared_instance = False self.is_connected_to_shared_instance = False
self.is_standalone_instance = False self.is_standalone_instance = False
self.jobs_thread = None self.jobs_thread = None
self.last_data_persist = time.time()
self.last_cache_clean = 0
if not os.path.isdir(Reticulum.storagepath): if not os.path.isdir(Reticulum.storagepath):
os.makedirs(Reticulum.storagepath) os.makedirs(Reticulum.storagepath)
@ -230,7 +234,6 @@ class Reticulum:
RNS.log("Could not load config file, creating default configuration file...") RNS.log("Could not load config file, creating default configuration file...")
self.__create_default_config() self.__create_default_config()
RNS.log("Default config file created. Make any necessary changes in "+Reticulum.configdir+"/config and restart Reticulum if needed.") RNS.log("Default config file created. Make any necessary changes in "+Reticulum.configdir+"/config and restart Reticulum if needed.")
import time
time.sleep(1.5) time.sleep(1.5)
self.__apply_config() self.__apply_config()
@ -261,8 +264,15 @@ class Reticulum:
def __jobs(self): def __jobs(self):
while True: while True:
# Clean caches now = time.time()
if now > self.last_cache_clean+Reticulum.CLEAN_INTERVAL:
self.__clean_caches() self.__clean_caches()
self.last_cache_clean = time.time()
if now > self.last_data_persist+Reticulum.PERSIST_INTERVAL:
self.__persist_data()
self.last_data_persist = time.time()
time.sleep(Reticulum.JOB_INTERVAL) time.sleep(Reticulum.JOB_INTERVAL)
@ -928,7 +938,9 @@ class Reticulum:
RNS.Transport.interfaces.append(interface) RNS.Transport.interfaces.append(interface)
def __persist_data(self):
RNS.Transport.persist_data()
RNS.Identity.persist_data()
def __clean_caches(self): def __clean_caches(self):
RNS.log("Cleaning resource and packet caches...", RNS.LOG_EXTREME) RNS.log("Cleaning resource and packet caches...", RNS.LOG_EXTREME)

View File

@ -2240,9 +2240,13 @@ class Transport:
Transport.saving_tunnel_table = False Transport.saving_tunnel_table = False
@staticmethod @staticmethod
def exit_handler(): def persist_data():
Transport.save_packet_hashlist() Transport.save_packet_hashlist()
Transport.save_path_table() Transport.save_path_table()
Transport.save_tunnel_table() Transport.save_tunnel_table()
@staticmethod
def exit_handler():
if not Transport.owner.is_connected_to_shared_instance:
Transport.persist_data()