mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-11-04 01:51:04 +01:00 
			
		
		
		
	Tear out all the complicated update logic
There is some weird behaviour that seem to storm the update channel. And our solution with a central map of update channels isnt particularly elegant. For now, replace all the complicated stuff with a simple channel that checks roughly every 10s if the node is up to date. Only generate and update if there has been changes.
This commit is contained in:
		
							parent
							
								
									6fb8d67825
								
							
						
					
					
						commit
						8abc7575cd
					
				
							
								
								
									
										33
									
								
								poll.go
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								poll.go
									
									
									
									
									
								
							@ -140,10 +140,9 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
				
			|||||||
		Str("id", c.Param("id")).
 | 
							Str("id", c.Param("id")).
 | 
				
			||||||
		Str("machine", m.Name).
 | 
							Str("machine", m.Name).
 | 
				
			||||||
		Msg("Loading or creating update channel")
 | 
							Msg("Loading or creating update channel")
 | 
				
			||||||
	updateChan := h.getOrOpenUpdateChannel(m)
 | 
						updateChan := make(chan struct{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pollDataChan := make(chan []byte)
 | 
						pollDataChan := make(chan []byte)
 | 
				
			||||||
	// defer close(pollData)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	keepAliveChan := make(chan []byte)
 | 
						keepAliveChan := make(chan []byte)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -160,7 +159,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
				
			|||||||
		// It sounds like we should update the nodes when we have received a endpoint update
 | 
							// It sounds like we should update the nodes when we have received a endpoint update
 | 
				
			||||||
		// even tho the comments in the tailscale code dont explicitly say so.
 | 
							// even tho the comments in the tailscale code dont explicitly say so.
 | 
				
			||||||
		updateRequestsFromNode.WithLabelValues("endpoint-update").Inc()
 | 
							updateRequestsFromNode.WithLabelValues("endpoint-update").Inc()
 | 
				
			||||||
		go h.notifyChangesToPeers(m)
 | 
							go func() { updateChan <- struct{}{} }()
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	} else if req.OmitPeers && req.Stream {
 | 
						} else if req.OmitPeers && req.Stream {
 | 
				
			||||||
		log.Warn().
 | 
							log.Warn().
 | 
				
			||||||
@ -186,7 +185,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
				
			|||||||
		Str("machine", m.Name).
 | 
							Str("machine", m.Name).
 | 
				
			||||||
		Msg("Notifying peers")
 | 
							Msg("Notifying peers")
 | 
				
			||||||
	updateRequestsFromNode.WithLabelValues("full-update").Inc()
 | 
						updateRequestsFromNode.WithLabelValues("full-update").Inc()
 | 
				
			||||||
	go h.notifyChangesToPeers(m)
 | 
						go func() { updateChan <- struct{}{} }()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	h.PollNetMapStream(c, m, req, mKey, pollDataChan, keepAliveChan, updateChan, cancelKeepAlive)
 | 
						h.PollNetMapStream(c, m, req, mKey, pollDataChan, keepAliveChan, updateChan, cancelKeepAlive)
 | 
				
			||||||
	log.Trace().
 | 
						log.Trace().
 | 
				
			||||||
@ -206,10 +205,10 @@ func (h *Headscale) PollNetMapStream(
 | 
				
			|||||||
	mKey wgkey.Key,
 | 
						mKey wgkey.Key,
 | 
				
			||||||
	pollDataChan chan []byte,
 | 
						pollDataChan chan []byte,
 | 
				
			||||||
	keepAliveChan chan []byte,
 | 
						keepAliveChan chan []byte,
 | 
				
			||||||
	updateChan <-chan struct{},
 | 
						updateChan chan struct{},
 | 
				
			||||||
	cancelKeepAlive chan struct{},
 | 
						cancelKeepAlive chan struct{},
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
	go h.scheduledPollWorker(cancelKeepAlive, keepAliveChan, mKey, req, m)
 | 
						go h.scheduledPollWorker(cancelKeepAlive, updateChan, keepAliveChan, mKey, req, m)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c.Stream(func(w io.Writer) bool {
 | 
						c.Stream(func(w io.Writer) bool {
 | 
				
			||||||
		log.Trace().
 | 
							log.Trace().
 | 
				
			||||||
@ -423,7 +422,8 @@ func (h *Headscale) PollNetMapStream(
 | 
				
			|||||||
				Str("machine", m.Name).
 | 
									Str("machine", m.Name).
 | 
				
			||||||
				Str("channel", "Done").
 | 
									Str("channel", "Done").
 | 
				
			||||||
				Msg("Closing update channel")
 | 
									Msg("Closing update channel")
 | 
				
			||||||
			h.closeUpdateChannel(m)
 | 
								//h.closeUpdateChannel(m)
 | 
				
			||||||
 | 
								close(updateChan)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			log.Trace().
 | 
								log.Trace().
 | 
				
			||||||
				Str("handler", "PollNetMapStream").
 | 
									Str("handler", "PollNetMapStream").
 | 
				
			||||||
@ -446,13 +446,14 @@ func (h *Headscale) PollNetMapStream(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (h *Headscale) scheduledPollWorker(
 | 
					func (h *Headscale) scheduledPollWorker(
 | 
				
			||||||
	cancelChan <-chan struct{},
 | 
						cancelChan <-chan struct{},
 | 
				
			||||||
 | 
						updateChan chan<- struct{},
 | 
				
			||||||
	keepAliveChan chan<- []byte,
 | 
						keepAliveChan chan<- []byte,
 | 
				
			||||||
	mKey wgkey.Key,
 | 
						mKey wgkey.Key,
 | 
				
			||||||
	req tailcfg.MapRequest,
 | 
						req tailcfg.MapRequest,
 | 
				
			||||||
	m *Machine,
 | 
						m *Machine,
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
	keepAliveTicker := time.NewTicker(60 * time.Second)
 | 
						keepAliveTicker := time.NewTicker(60 * time.Second)
 | 
				
			||||||
	updateCheckerTicker := time.NewTicker(30 * time.Second)
 | 
						updateCheckerTicker := time.NewTicker(10 * time.Second)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		select {
 | 
							select {
 | 
				
			||||||
@ -476,16 +477,12 @@ func (h *Headscale) scheduledPollWorker(
 | 
				
			|||||||
			keepAliveChan <- data
 | 
								keepAliveChan <- data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		case <-updateCheckerTicker.C:
 | 
							case <-updateCheckerTicker.C:
 | 
				
			||||||
			// Send an update request regardless of outdated or not, if data is sent
 | 
								log.Debug().
 | 
				
			||||||
			// to the node is determined in the updateChan consumer block
 | 
									Str("func", "scheduledPollWorker").
 | 
				
			||||||
			err := h.sendRequestOnUpdateChannel(m)
 | 
									Str("machine", m.Name).
 | 
				
			||||||
			if err != nil {
 | 
									Msg("Sending update request")
 | 
				
			||||||
				log.Error().
 | 
								updateRequestsFromNode.WithLabelValues("scheduled-update").Inc()
 | 
				
			||||||
					Str("func", "keepAlive").
 | 
								updateChan <- struct{}{}
 | 
				
			||||||
					Str("machine", m.Name).
 | 
					 | 
				
			||||||
					Err(err).
 | 
					 | 
				
			||||||
					Msgf("Failed to send update request to %s", m.Name)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user