mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	reintroduce (better) dht throttling
This commit is contained in:
		
							parent
							
								
									8825494d59
								
							
						
					
					
						commit
						95201669fe
					
				
					 1 changed files with 23 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -15,6 +15,7 @@ type dhtInfo struct {
 | 
			
		|||
	coords        []byte
 | 
			
		||||
	recv          time.Time // When we last received a message
 | 
			
		||||
	pings         int       // Time out if at least 3 consecutive maintenance pings drop
 | 
			
		||||
	throttle      time.Duration
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Returns the *NodeID associated with dhtInfo.key, calculating it on the fly the first time or from a cache all subsequent times.
 | 
			
		||||
| 
						 | 
				
			
			@ -123,6 +124,22 @@ func (t *dht) insert(info *dhtInfo) {
 | 
			
		|||
		panic("FIXME")
 | 
			
		||||
	}
 | 
			
		||||
	info.recv = time.Now()
 | 
			
		||||
	if oldInfo, isIn := t.table[*info.getNodeID()]; isIn {
 | 
			
		||||
		sameCoords := true
 | 
			
		||||
		if len(info.coords) != len(oldInfo.coords) {
 | 
			
		||||
			sameCoords = false
 | 
			
		||||
		} else {
 | 
			
		||||
			for idx := 0; idx < len(info.coords); idx++ {
 | 
			
		||||
				if info.coords[idx] != oldInfo.coords[idx] {
 | 
			
		||||
					sameCoords = false
 | 
			
		||||
					break
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		if sameCoords {
 | 
			
		||||
			info.throttle = oldInfo.throttle
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	t.table[*info.getNodeID()] = info
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -309,8 +326,13 @@ func (t *dht) doMaintenance() {
 | 
			
		|||
			successor = info
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if successor != nil {
 | 
			
		||||
	if successor != nil &&
 | 
			
		||||
		now.Sub(successor.recv) > successor.throttle {
 | 
			
		||||
		t.ping(successor, nil)
 | 
			
		||||
		successor.pings++
 | 
			
		||||
		successor.throttle += time.Second
 | 
			
		||||
		if successor.throttle > 30*time.Second {
 | 
			
		||||
			successor.throttle = 30 * time.Second
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue