search timing changes

This commit is contained in:
Arceliar 2020-02-06 18:37:58 -06:00
parent b8bab06f95
commit cd856426e5

View File

@ -27,6 +27,7 @@ const search_MAX_SEARCH_SIZE = 16
// This defines the time after which we time out a search (so it can restart). // This defines the time after which we time out a search (so it can restart).
const search_RETRY_TIME = 3 * time.Second const search_RETRY_TIME = 3 * time.Second
const search_STEP_TIME = 100 * time.Millisecond
// Information about an ongoing search. // Information about an ongoing search.
// Includes the target NodeID, the bitmask to match it to an IP, and the list of nodes to visit / already visited. // Includes the target NodeID, the bitmask to match it to an IP, and the list of nodes to visit / already visited.
@ -78,14 +79,20 @@ func (s *searches) createSearch(dest *crypto.NodeID, mask *crypto.NodeID, callba
// If there is, it adds the response info to the search and triggers a new search step. // If there is, it adds the response info to the search and triggers a new search step.
// If there's no ongoing search, or we if the dhtRes finished the search (it was from the target node), then don't do anything more. // If there's no ongoing search, or we if the dhtRes finished the search (it was from the target node), then don't do anything more.
func (sinfo *searchInfo) handleDHTRes(res *dhtRes) { func (sinfo *searchInfo) handleDHTRes(res *dhtRes) {
sinfo.recv++ old := sinfo.visited
if res == nil || sinfo.checkDHTRes(res) { if res != nil {
// Either we don't recognize this search, or we just finished it sinfo.recv++
return if sinfo.checkDHTRes(res) {
sinfo.searches.router.core.log.Debugln("Finished search:", sinfo.dest, sinfo.send, sinfo.recv)
return // Search finished successfully
}
// Add results to the search
sinfo.addToSearch(res)
}
if res == nil || sinfo.visited != old {
// Continue the search
sinfo.doSearchStep()
} }
// Add to the search and continue
sinfo.addToSearch(res)
sinfo.doSearchStep()
} }
// Adds the information from a dhtRes to an ongoing search. // Adds the information from a dhtRes to an ongoing search.
@ -104,7 +111,7 @@ func (sinfo *searchInfo) addToSearch(res *dhtRes) {
// Sort // Sort
sort.SliceStable(sinfo.toVisit, func(i, j int) bool { sort.SliceStable(sinfo.toVisit, func(i, j int) bool {
// Should return true if i is closer to the destination than j // Should return true if i is closer to the destination than j
return dht_ordered(&res.Dest, sinfo.toVisit[i].getNodeID(), sinfo.toVisit[j].getNodeID()) return dht_ordered(&sinfo.dest, sinfo.toVisit[i].getNodeID(), sinfo.toVisit[j].getNodeID())
}) })
} }
} }
@ -121,14 +128,14 @@ func (sinfo *searchInfo) doSearchStep() {
return return
} }
// Send to the next search target // Send to the next search target
for _, next := range sinfo.toVisit { if len(sinfo.toVisit) > 0 {
next := sinfo.toVisit[0]
sinfo.toVisit = sinfo.toVisit[1:]
rq := dhtReqKey{next.key, sinfo.dest} rq := dhtReqKey{next.key, sinfo.dest}
sinfo.searches.router.dht.addCallback(&rq, sinfo.handleDHTRes) sinfo.searches.router.dht.addCallback(&rq, sinfo.handleDHTRes)
sinfo.searches.router.dht.ping(next, &sinfo.dest) sinfo.searches.router.dht.ping(next, &sinfo.dest)
sinfo.time = time.Now()
sinfo.send++ sinfo.send++
} }
sinfo.toVisit = sinfo.toVisit[:0]
} }
// If we've recently sent a ping for this search, do nothing. // If we've recently sent a ping for this search, do nothing.
@ -138,7 +145,7 @@ func (sinfo *searchInfo) continueSearch() {
// In case the search dies, try to spawn another thread later // In case the search dies, try to spawn another thread later
// Note that this will spawn multiple parallel searches as time passes // Note that this will spawn multiple parallel searches as time passes
// Any that die aren't restarted, but a new one will start later // Any that die aren't restarted, but a new one will start later
time.AfterFunc(search_RETRY_TIME, func() { time.AfterFunc(search_STEP_TIME, func() {
sinfo.searches.router.Act(nil, func() { sinfo.searches.router.Act(nil, func() {
// FIXME this keeps the search alive forever if not for the searches map, fix that // FIXME this keeps the search alive forever if not for the searches map, fix that
newSearchInfo := sinfo.searches.searches[sinfo.dest] newSearchInfo := sinfo.searches.searches[sinfo.dest]
@ -171,6 +178,7 @@ func (sinfo *searchInfo) checkDHTRes(res *dhtRes) bool {
// Closer to the destination, so update visited // Closer to the destination, so update visited
sinfo.searches.router.core.log.Debugln("Updating search:", sinfo.dest, *from.getNodeID(), sinfo.send, sinfo.recv) sinfo.searches.router.core.log.Debugln("Updating search:", sinfo.dest, *from.getNodeID(), sinfo.send, sinfo.recv)
sinfo.visited = *from.getNodeID() sinfo.visited = *from.getNodeID()
sinfo.time = time.Now()
} }
them := from.getNodeID() them := from.getNodeID()
var destMasked crypto.NodeID var destMasked crypto.NodeID