mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	refactor searches to store a pointer to router instead of core
This commit is contained in:
		
							parent
							
								
									e7024a00e7
								
							
						
					
					
						commit
						5bb85cf07b
					
				
					 2 changed files with 20 additions and 20 deletions
				
			
		| 
						 | 
					@ -74,7 +74,7 @@ func (r *router) init(core *Core) {
 | 
				
			||||||
	r.nodeinfo.setNodeInfo(r.core.config.Current.NodeInfo, r.core.config.Current.NodeInfoPrivacy)
 | 
						r.nodeinfo.setNodeInfo(r.core.config.Current.NodeInfo, r.core.config.Current.NodeInfoPrivacy)
 | 
				
			||||||
	r.core.config.Mutex.RUnlock()
 | 
						r.core.config.Mutex.RUnlock()
 | 
				
			||||||
	r.dht.init(r)
 | 
						r.dht.init(r)
 | 
				
			||||||
	r.searches.init(r.core)
 | 
						r.searches.init(r)
 | 
				
			||||||
	r.sessions.init(r.core)
 | 
						r.sessions.init(r.core)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -33,7 +33,7 @@ const search_RETRY_TIME = time.Second
 | 
				
			||||||
// 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.
 | 
				
			||||||
type searchInfo struct {
 | 
					type searchInfo struct {
 | 
				
			||||||
	core     *Core
 | 
						searches *searches
 | 
				
			||||||
	dest     crypto.NodeID
 | 
						dest     crypto.NodeID
 | 
				
			||||||
	mask     crypto.NodeID
 | 
						mask     crypto.NodeID
 | 
				
			||||||
	time     time.Time
 | 
						time     time.Time
 | 
				
			||||||
| 
						 | 
					@ -45,14 +45,14 @@ type searchInfo struct {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// This stores a map of active searches.
 | 
					// This stores a map of active searches.
 | 
				
			||||||
type searches struct {
 | 
					type searches struct {
 | 
				
			||||||
	core        *Core
 | 
						router      *router
 | 
				
			||||||
	reconfigure chan chan error
 | 
						reconfigure chan chan error
 | 
				
			||||||
	searches    map[crypto.NodeID]*searchInfo
 | 
						searches    map[crypto.NodeID]*searchInfo
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Initializes the searches struct.
 | 
					// Initializes the searches struct.
 | 
				
			||||||
func (s *searches) init(core *Core) {
 | 
					func (s *searches) init(r *router) {
 | 
				
			||||||
	s.core = core
 | 
						s.router = r
 | 
				
			||||||
	s.reconfigure = make(chan chan error, 1)
 | 
						s.reconfigure = make(chan chan error, 1)
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		for {
 | 
							for {
 | 
				
			||||||
| 
						 | 
					@ -66,7 +66,7 @@ func (s *searches) init(core *Core) {
 | 
				
			||||||
// Creates a new search info, adds it to the searches struct, and returns a pointer to the info.
 | 
					// Creates a new search info, adds it to the searches struct, and returns a pointer to the info.
 | 
				
			||||||
func (s *searches) createSearch(dest *crypto.NodeID, mask *crypto.NodeID, callback func(*sessionInfo, error)) *searchInfo {
 | 
					func (s *searches) createSearch(dest *crypto.NodeID, mask *crypto.NodeID, callback func(*sessionInfo, error)) *searchInfo {
 | 
				
			||||||
	info := searchInfo{
 | 
						info := searchInfo{
 | 
				
			||||||
		core:     s.core,
 | 
							searches: s,
 | 
				
			||||||
		dest:     *dest,
 | 
							dest:     *dest,
 | 
				
			||||||
		mask:     *mask,
 | 
							mask:     *mask,
 | 
				
			||||||
		time:     time.Now(),
 | 
							time:     time.Now(),
 | 
				
			||||||
| 
						 | 
					@ -100,7 +100,7 @@ func (sinfo *searchInfo) addToSearch(res *dhtRes) {
 | 
				
			||||||
	from := dhtInfo{key: res.Key, coords: res.Coords}
 | 
						from := dhtInfo{key: res.Key, coords: res.Coords}
 | 
				
			||||||
	sinfo.visited[*from.getNodeID()] = true
 | 
						sinfo.visited[*from.getNodeID()] = true
 | 
				
			||||||
	for _, info := range res.Infos {
 | 
						for _, info := range res.Infos {
 | 
				
			||||||
		if *info.getNodeID() == sinfo.core.router.dht.nodeID || sinfo.visited[*info.getNodeID()] {
 | 
							if *info.getNodeID() == sinfo.searches.router.dht.nodeID || sinfo.visited[*info.getNodeID()] {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if dht_ordered(&sinfo.dest, info.getNodeID(), from.getNodeID()) {
 | 
							if dht_ordered(&sinfo.dest, info.getNodeID(), from.getNodeID()) {
 | 
				
			||||||
| 
						 | 
					@ -134,7 +134,7 @@ func (sinfo *searchInfo) doSearchStep() {
 | 
				
			||||||
	if len(sinfo.toVisit) == 0 {
 | 
						if len(sinfo.toVisit) == 0 {
 | 
				
			||||||
		if time.Since(sinfo.time) > search_RETRY_TIME {
 | 
							if time.Since(sinfo.time) > search_RETRY_TIME {
 | 
				
			||||||
			// Dead end and no response in too long, do cleanup
 | 
								// Dead end and no response in too long, do cleanup
 | 
				
			||||||
			delete(sinfo.core.router.searches.searches, sinfo.dest)
 | 
								delete(sinfo.searches.searches, sinfo.dest)
 | 
				
			||||||
			sinfo.callback(nil, errors.New("search reached dead end"))
 | 
								sinfo.callback(nil, errors.New("search reached dead end"))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
| 
						 | 
					@ -143,8 +143,8 @@ func (sinfo *searchInfo) doSearchStep() {
 | 
				
			||||||
	var next *dhtInfo
 | 
						var next *dhtInfo
 | 
				
			||||||
	next, sinfo.toVisit = sinfo.toVisit[0], sinfo.toVisit[1:]
 | 
						next, sinfo.toVisit = sinfo.toVisit[0], sinfo.toVisit[1:]
 | 
				
			||||||
	rq := dhtReqKey{next.key, sinfo.dest}
 | 
						rq := dhtReqKey{next.key, sinfo.dest}
 | 
				
			||||||
	sinfo.core.router.dht.addCallback(&rq, sinfo.handleDHTRes)
 | 
						sinfo.searches.router.dht.addCallback(&rq, sinfo.handleDHTRes)
 | 
				
			||||||
	sinfo.core.router.dht.ping(next, &sinfo.dest)
 | 
						sinfo.searches.router.dht.ping(next, &sinfo.dest)
 | 
				
			||||||
	sinfo.time = time.Now()
 | 
						sinfo.time = time.Now()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -157,7 +157,7 @@ func (sinfo *searchInfo) continueSearch() {
 | 
				
			||||||
	// 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
 | 
				
			||||||
	retryLater := func() {
 | 
						retryLater := 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.core.router.searches.searches[sinfo.dest]
 | 
							newSearchInfo := sinfo.searches.searches[sinfo.dest]
 | 
				
			||||||
		if newSearchInfo != sinfo {
 | 
							if newSearchInfo != sinfo {
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -165,7 +165,7 @@ func (sinfo *searchInfo) continueSearch() {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		time.Sleep(search_RETRY_TIME)
 | 
							time.Sleep(search_RETRY_TIME)
 | 
				
			||||||
		sinfo.core.router.doAdmin(retryLater)
 | 
							sinfo.searches.router.doAdmin(retryLater)
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -173,9 +173,9 @@ func (sinfo *searchInfo) continueSearch() {
 | 
				
			||||||
func (s *searches) newIterSearch(dest *crypto.NodeID, mask *crypto.NodeID, callback func(*sessionInfo, error)) *searchInfo {
 | 
					func (s *searches) newIterSearch(dest *crypto.NodeID, mask *crypto.NodeID, callback func(*sessionInfo, error)) *searchInfo {
 | 
				
			||||||
	sinfo := s.createSearch(dest, mask, callback)
 | 
						sinfo := s.createSearch(dest, mask, callback)
 | 
				
			||||||
	sinfo.visited = make(map[crypto.NodeID]bool)
 | 
						sinfo.visited = make(map[crypto.NodeID]bool)
 | 
				
			||||||
	loc := s.core.switchTable.getLocator()
 | 
						loc := s.router.core.switchTable.getLocator()
 | 
				
			||||||
	sinfo.toVisit = append(sinfo.toVisit, &dhtInfo{
 | 
						sinfo.toVisit = append(sinfo.toVisit, &dhtInfo{
 | 
				
			||||||
		key:    s.core.boxPub,
 | 
							key:    s.router.core.boxPub,
 | 
				
			||||||
		coords: loc.getCoords(),
 | 
							coords: loc.getCoords(),
 | 
				
			||||||
	}) // Start the search by asking ourself, useful if we're the destination
 | 
						}) // Start the search by asking ourself, useful if we're the destination
 | 
				
			||||||
	return sinfo
 | 
						return sinfo
 | 
				
			||||||
| 
						 | 
					@ -196,26 +196,26 @@ func (sinfo *searchInfo) checkDHTRes(res *dhtRes) bool {
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// They match, so create a session and send a sessionRequest
 | 
						// They match, so create a session and send a sessionRequest
 | 
				
			||||||
	sess, isIn := sinfo.core.router.sessions.getByTheirPerm(&res.Key)
 | 
						sess, isIn := sinfo.searches.router.sessions.getByTheirPerm(&res.Key)
 | 
				
			||||||
	if !isIn {
 | 
						if !isIn {
 | 
				
			||||||
		sess = sinfo.core.router.sessions.createSession(&res.Key)
 | 
							sess = sinfo.searches.router.sessions.createSession(&res.Key)
 | 
				
			||||||
		if sess == nil {
 | 
							if sess == nil {
 | 
				
			||||||
			// nil if the DHT search finished but the session wasn't allowed
 | 
								// nil if the DHT search finished but the session wasn't allowed
 | 
				
			||||||
			sinfo.callback(nil, errors.New("session not allowed"))
 | 
								sinfo.callback(nil, errors.New("session not allowed"))
 | 
				
			||||||
			// Cleanup
 | 
								// Cleanup
 | 
				
			||||||
			delete(sinfo.core.router.searches.searches, res.Dest)
 | 
								delete(sinfo.searches.searches, res.Dest)
 | 
				
			||||||
			return true
 | 
								return true
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		_, isIn := sinfo.core.router.sessions.getByTheirPerm(&res.Key)
 | 
							_, isIn := sinfo.searches.router.sessions.getByTheirPerm(&res.Key)
 | 
				
			||||||
		if !isIn {
 | 
							if !isIn {
 | 
				
			||||||
			panic("This should never happen")
 | 
								panic("This should never happen")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// FIXME (!) replay attacks could mess with coords? Give it a handle (tstamp)?
 | 
						// FIXME (!) replay attacks could mess with coords? Give it a handle (tstamp)?
 | 
				
			||||||
	sess.coords = res.Coords
 | 
						sess.coords = res.Coords
 | 
				
			||||||
	sess.ping(&sinfo.core.router)
 | 
						sess.ping(sinfo.searches.router)
 | 
				
			||||||
	sinfo.callback(sess, nil)
 | 
						sinfo.callback(sess, nil)
 | 
				
			||||||
	// Cleanup
 | 
						// Cleanup
 | 
				
			||||||
	delete(sinfo.core.router.searches.searches, res.Dest)
 | 
						delete(sinfo.searches.searches, res.Dest)
 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue