mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	let the peer's linkLoop call close if the peer receives no announcements for too long
This commit is contained in:
		
							parent
							
								
									6026e0a014
								
							
						
					
					
						commit
						80f893aac3
					
				
					 2 changed files with 38 additions and 31 deletions
				
			
		| 
						 | 
					@ -82,6 +82,8 @@ type peer struct {
 | 
				
			||||||
	throttle uint8
 | 
						throttle uint8
 | 
				
			||||||
	// Called when a peer is removed, to close the underlying connection, or via admin api
 | 
						// Called when a peer is removed, to close the underlying connection, or via admin api
 | 
				
			||||||
	close func()
 | 
						close func()
 | 
				
			||||||
 | 
						// To allow the peer to call close if idle for too long
 | 
				
			||||||
 | 
						lastAnc time.Time
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const peer_Throttle = 1
 | 
					const peer_Throttle = 1
 | 
				
			||||||
| 
						 | 
					@ -106,14 +108,11 @@ func (p *peer) updateBandwidth(bytes int, duration time.Duration) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (ps *peers) newPeer(box *boxPubKey,
 | 
					func (ps *peers) newPeer(box *boxPubKey,
 | 
				
			||||||
	sig *sigPubKey) *peer {
 | 
						sig *sigPubKey) *peer {
 | 
				
			||||||
	//in <-chan []byte,
 | 
					 | 
				
			||||||
	//out chan<- []byte) *peer {
 | 
					 | 
				
			||||||
	p := peer{box: *box,
 | 
						p := peer{box: *box,
 | 
				
			||||||
		sig:    *sig,
 | 
							sig:     *sig,
 | 
				
			||||||
		shared: *getSharedKey(&ps.core.boxPriv, box),
 | 
							shared:  *getSharedKey(&ps.core.boxPriv, box),
 | 
				
			||||||
		//in: in,
 | 
							lastAnc: time.Now(),
 | 
				
			||||||
		//out: out,
 | 
							core:    ps.core}
 | 
				
			||||||
		core: ps.core}
 | 
					 | 
				
			||||||
	ps.mutex.Lock()
 | 
						ps.mutex.Lock()
 | 
				
			||||||
	defer ps.mutex.Unlock()
 | 
						defer ps.mutex.Unlock()
 | 
				
			||||||
	oldPorts := ps.getPorts()
 | 
						oldPorts := ps.getPorts()
 | 
				
			||||||
| 
						 | 
					@ -165,31 +164,33 @@ func (p *peer) linkLoop(in <-chan []byte) {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			p.handleLinkTraffic(packet)
 | 
								p.handleLinkTraffic(packet)
 | 
				
			||||||
		case <-ticker.C:
 | 
							case <-ticker.C:
 | 
				
			||||||
			{
 | 
								if time.Since(p.lastAnc) > 16*time.Second && p.close != nil {
 | 
				
			||||||
				p.throttle = 0
 | 
									// Seems to have timed out, try to trigger a close
 | 
				
			||||||
				if p.port == 0 {
 | 
									p.close()
 | 
				
			||||||
					continue
 | 
					 | 
				
			||||||
				} // Don't send announces on selfInterface
 | 
					 | 
				
			||||||
				p.myMsg, p.mySigs = p.core.switchTable.createMessage(p.port)
 | 
					 | 
				
			||||||
				var update bool
 | 
					 | 
				
			||||||
				switch {
 | 
					 | 
				
			||||||
				case p.msgAnc == nil:
 | 
					 | 
				
			||||||
					update = true
 | 
					 | 
				
			||||||
				case lastRSeq != p.msgAnc.seq:
 | 
					 | 
				
			||||||
					update = true
 | 
					 | 
				
			||||||
				case p.msgAnc.rseq != p.myMsg.seq:
 | 
					 | 
				
			||||||
					update = true
 | 
					 | 
				
			||||||
				case counter%4 == 0:
 | 
					 | 
				
			||||||
					update = true
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				if update {
 | 
					 | 
				
			||||||
					if p.msgAnc != nil {
 | 
					 | 
				
			||||||
						lastRSeq = p.msgAnc.seq
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
					p.sendSwitchAnnounce()
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				counter = (counter + 1) % 4
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
								p.throttle = 0
 | 
				
			||||||
 | 
								if p.port == 0 {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								} // Don't send announces on selfInterface
 | 
				
			||||||
 | 
								p.myMsg, p.mySigs = p.core.switchTable.createMessage(p.port)
 | 
				
			||||||
 | 
								var update bool
 | 
				
			||||||
 | 
								switch {
 | 
				
			||||||
 | 
								case p.msgAnc == nil:
 | 
				
			||||||
 | 
									update = true
 | 
				
			||||||
 | 
								case lastRSeq != p.msgAnc.seq:
 | 
				
			||||||
 | 
									update = true
 | 
				
			||||||
 | 
								case p.msgAnc.rseq != p.myMsg.seq:
 | 
				
			||||||
 | 
									update = true
 | 
				
			||||||
 | 
								case counter%4 == 0:
 | 
				
			||||||
 | 
									update = true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if update {
 | 
				
			||||||
 | 
									if p.msgAnc != nil {
 | 
				
			||||||
 | 
										lastRSeq = p.msgAnc.seq
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									p.sendSwitchAnnounce()
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								counter = (counter + 1) % 4
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -217,6 +218,10 @@ func (p *peer) handlePacket(packet []byte, linkIn chan<- []byte) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *peer) handleTraffic(packet []byte, pTypeLen int) {
 | 
					func (p *peer) handleTraffic(packet []byte, pTypeLen int) {
 | 
				
			||||||
 | 
						if p.msgAnc == nil {
 | 
				
			||||||
 | 
							// Drop traffic until the peer manages to send us at least one anc
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	ttl, ttlLen := wire_decode_uint64(packet[pTypeLen:])
 | 
						ttl, ttlLen := wire_decode_uint64(packet[pTypeLen:])
 | 
				
			||||||
	ttlBegin := pTypeLen
 | 
						ttlBegin := pTypeLen
 | 
				
			||||||
	ttlEnd := pTypeLen + ttlLen
 | 
						ttlEnd := pTypeLen + ttlLen
 | 
				
			||||||
| 
						 | 
					@ -299,6 +304,7 @@ func (p *peer) handleSwitchAnnounce(packet []byte) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	p.msgAnc = &anc
 | 
						p.msgAnc = &anc
 | 
				
			||||||
	p.processSwitchMessage()
 | 
						p.processSwitchMessage()
 | 
				
			||||||
 | 
						p.lastAnc = time.Now()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (p *peer) requestHop(hop uint64) {
 | 
					func (p *peer) requestHop(hop uint64) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -104,6 +104,7 @@ func generateConfig(isAutoconf bool) *nodeConfig {
 | 
				
			||||||
	cfg.SigPub = hex.EncodeToString(spub[:])
 | 
						cfg.SigPub = hex.EncodeToString(spub[:])
 | 
				
			||||||
	cfg.SigPriv = hex.EncodeToString(spriv[:])
 | 
						cfg.SigPriv = hex.EncodeToString(spriv[:])
 | 
				
			||||||
	cfg.Peers = []string{}
 | 
						cfg.Peers = []string{}
 | 
				
			||||||
 | 
						cfg.PeerBoxPubs = []string{}
 | 
				
			||||||
	cfg.Multicast = true
 | 
						cfg.Multicast = true
 | 
				
			||||||
	cfg.LinkLocal = ""
 | 
						cfg.LinkLocal = ""
 | 
				
			||||||
	cfg.IfName = core.DEBUG_GetTUNDefaultIfName()
 | 
						cfg.IfName = core.DEBUG_GetTUNDefaultIfName()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue