mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	add a throttle to nodes in the dht. the throttle is gradually increased each time the node is pinged. it determines the minimum amount of time to wait between using the node in a bootstrapping search
This commit is contained in:
		
							parent
							
								
									ec1c173ca5
								
							
						
					
					
						commit
						fe12e1509a
					
				
					 1 changed files with 12 additions and 5 deletions
				
			
		| 
						 | 
					@ -36,6 +36,7 @@ type dhtInfo struct {
 | 
				
			||||||
	send          time.Time // When we last sent a message
 | 
						send          time.Time // When we last sent a message
 | 
				
			||||||
	recv          time.Time // When we last received a message
 | 
						recv          time.Time // When we last received a message
 | 
				
			||||||
	pings         int       // Decide when to drop
 | 
						pings         int       // Decide when to drop
 | 
				
			||||||
 | 
						throttle      uint8     // Number of seconds to wait before pinging a node to bootstrap buckets, gradually increases up to 1 minute
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (info *dhtInfo) getNodeID() *NodeID {
 | 
					func (info *dhtInfo) getNodeID() *NodeID {
 | 
				
			||||||
| 
						 | 
					@ -120,6 +121,7 @@ func (t *dht) handleRes(res *dhtRes) {
 | 
				
			||||||
		coords:   res.Coords,
 | 
							coords:   res.Coords,
 | 
				
			||||||
		send:     time.Now(), // Technically wrong but should be OK...
 | 
							send:     time.Now(), // Technically wrong but should be OK...
 | 
				
			||||||
		recv:     time.Now(),
 | 
							recv:     time.Now(),
 | 
				
			||||||
 | 
							throttle: 1,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// If they're already in the table, then keep the correct send time
 | 
						// If they're already in the table, then keep the correct send time
 | 
				
			||||||
	bidx, isOK := t.getBucketIndex(rinfo.getNodeID())
 | 
						bidx, isOK := t.getBucketIndex(rinfo.getNodeID())
 | 
				
			||||||
| 
						 | 
					@ -130,11 +132,13 @@ func (t *dht) handleRes(res *dhtRes) {
 | 
				
			||||||
	for _, oldinfo := range b.peers {
 | 
						for _, oldinfo := range b.peers {
 | 
				
			||||||
		if oldinfo.key == rinfo.key {
 | 
							if oldinfo.key == rinfo.key {
 | 
				
			||||||
			rinfo.send = oldinfo.send
 | 
								rinfo.send = oldinfo.send
 | 
				
			||||||
 | 
								rinfo.throttle += oldinfo.throttle
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for _, oldinfo := range b.other {
 | 
						for _, oldinfo := range b.other {
 | 
				
			||||||
		if oldinfo.key == rinfo.key {
 | 
							if oldinfo.key == rinfo.key {
 | 
				
			||||||
			rinfo.send = oldinfo.send
 | 
								rinfo.send = oldinfo.send
 | 
				
			||||||
 | 
								rinfo.throttle += oldinfo.throttle
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Insert into table
 | 
						// Insert into table
 | 
				
			||||||
| 
						 | 
					@ -231,6 +235,9 @@ func (t *dht) insert(info *dhtInfo, isPeer bool) {
 | 
				
			||||||
		// This speeds up bootstrapping
 | 
							// This speeds up bootstrapping
 | 
				
			||||||
		info.recv = info.recv.Add(-time.Hour)
 | 
							info.recv = info.recv.Add(-time.Hour)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						if isPeer || info.throttle > 60 {
 | 
				
			||||||
 | 
							info.throttle = 60
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	// First drop any existing entry from the bucket
 | 
						// First drop any existing entry from the bucket
 | 
				
			||||||
	b.drop(&info.key)
 | 
						b.drop(&info.key)
 | 
				
			||||||
	// Now add to the *end* of the bucket
 | 
						// Now add to the *end* of the bucket
 | 
				
			||||||
| 
						 | 
					@ -460,7 +467,7 @@ func (t *dht) doMaintenance() {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		target := t.getTarget(t.offset)
 | 
							target := t.getTarget(t.offset)
 | 
				
			||||||
		for _, info := range t.lookup(target, true) {
 | 
							for _, info := range t.lookup(target, true) {
 | 
				
			||||||
			if time.Since(info.recv) > time.Minute {
 | 
								if time.Since(info.recv) > time.Duration(info.throttle)*time.Second {
 | 
				
			||||||
				t.addToMill(info, target)
 | 
									t.addToMill(info, target)
 | 
				
			||||||
				t.offset++
 | 
									t.offset++
 | 
				
			||||||
				break
 | 
									break
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue