use looping call for Node.change_token to avoid hanging delayedCalls

This commit is contained in:
Jack Robison 2017-10-10 13:10:47 -04:00
parent f1980f524e
commit 8e9f3c90a1
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF

View file

@ -12,7 +12,7 @@ import operator
import struct import struct
import time import time
from twisted.internet import defer, error, reactor, threads from twisted.internet import defer, error, reactor, threads, task
import constants import constants
import routingtable import routingtable
@ -88,7 +88,7 @@ class Node(object):
# operations before the node has finished joining the network) # operations before the node has finished joining the network)
self._joinDeferred = None self._joinDeferred = None
self.next_refresh_call = None self.next_refresh_call = None
self.next_change_token_call = None self.change_token_lc = task.LoopingCall(self.change_token)
# Create k-buckets (for storing contacts) # Create k-buckets (for storing contacts)
if routingTableClass is None: if routingTableClass is None:
self._routingTable = routingtable.OptimizedTreeRoutingTable(self.id) self._routingTable = routingtable.OptimizedTreeRoutingTable(self.id)
@ -103,7 +103,6 @@ class Node(object):
# Initialize the data storage mechanism used by this node # Initialize the data storage mechanism used by this node
self.token_secret = self._generateID() self.token_secret = self._generateID()
self.old_token_secret = None self.old_token_secret = None
self.change_token()
if dataStore is None: if dataStore is None:
self._dataStore = datastore.DictDataStore() self._dataStore = datastore.DictDataStore()
else: else:
@ -128,9 +127,8 @@ class Node(object):
if self.next_refresh_call is not None: if self.next_refresh_call is not None:
self.next_refresh_call.cancel() self.next_refresh_call.cancel()
self.next_refresh_call = None self.next_refresh_call = None
if self.next_change_token_call is not None: if self.change_token_lc.running:
self.next_change_token_call.cancel() self.change_token_lc.stop()
self.next_change_token_call = None
if self._listeningPort is not None: if self._listeningPort is not None:
self._listeningPort.stopListening() self._listeningPort.stopListening()
self.hash_watcher.stop() self.hash_watcher.stop()
@ -163,6 +161,10 @@ class Node(object):
bootstrapContacts.append(contact) bootstrapContacts.append(contact)
else: else:
bootstrapContacts = None bootstrapContacts = None
# Start the token looping call
self.change_token_lc.start(constants.tokenSecretChangeInterval)
# Initiate the Kademlia joining sequence - perform a search for this node's own ID # Initiate the Kademlia joining sequence - perform a search for this node's own ID
self._joinDeferred = self._iterativeFind(self.id, bootstrapContacts) self._joinDeferred = self._iterativeFind(self.id, bootstrapContacts)
# #TODO: Refresh all k-buckets further away than this node's closest neighbour # #TODO: Refresh all k-buckets further away than this node's closest neighbour
@ -295,8 +297,6 @@ class Node(object):
def change_token(self): def change_token(self):
self.old_token_secret = self.token_secret self.old_token_secret = self.token_secret
self.token_secret = self._generateID() self.token_secret = self._generateID()
self.next_change_token_call = reactor.callLater(constants.tokenSecretChangeInterval,
self.change_token)
def make_token(self, compact_ip): def make_token(self, compact_ip):
h = hashlib.new('sha384') h = hashlib.new('sha384')