Move isOutdated logic to updateChan consumation

This commit is contained in:
Kristoffer Dalby 2021-08-19 18:05:33 +01:00
parent dd8c0d1e9e
commit 8d1adaaef3
No known key found for this signature in database
GPG Key ID: 09F62DC067465735
2 changed files with 26 additions and 25 deletions

View File

@ -315,6 +315,11 @@ func (h *Headscale) requestUpdate(m *tailcfg.Node) error {
} }
func (h *Headscale) isOutdated(m *Machine) bool { func (h *Headscale) isOutdated(m *Machine) bool {
err := h.UpdateMachine(m)
if err != nil {
return true
}
lastChange := h.getLastStateChange() lastChange := h.getLastStateChange()
log.Trace(). log.Trace().
Str("func", "keepAlive"). Str("func", "keepAlive").

46
poll.go
View File

@ -300,12 +300,18 @@ func (h *Headscale) PollNetMapStream(
return true return true
case <-updateChan: case <-updateChan:
log.Trace().
Str("handler", "PollNetMapStream").
Str("machine", m.Name).
Str("channel", "update").
Msg("Received a request for update")
if h.isOutdated(&m) { if h.isOutdated(&m) {
log.Trace(). log.Debug().
Str("handler", "PollNetMapStream"). Str("handler", "PollNetMapStream").
Str("machine", m.Name). Str("machine", m.Name).
Str("channel", "update"). Time("last_successful_update", *m.LastSuccessfulUpdate).
Msg("Received a request for update") Time("last_state_change", h.getLastStateChange()).
Msgf("There has been updates since the last successful update to %s", m.Name)
data, err := h.getMapResponse(mKey, req, m) data, err := h.getMapResponse(mKey, req, m)
if err != nil { if err != nil {
log.Error(). log.Error().
@ -337,6 +343,13 @@ func (h *Headscale) PollNetMapStream(
now := time.Now().UTC() now := time.Now().UTC()
m.LastSuccessfulUpdate = &now m.LastSuccessfulUpdate = &now
h.db.Save(&m) h.db.Save(&m)
} else {
log.Trace().
Str("handler", "PollNetMapStream").
Str("machine", m.Name).
Time("last_successful_update", *m.LastSuccessfulUpdate).
Time("last_state_change", h.getLastStateChange()).
Msgf("%s is up to date", m.Name)
} }
return true return true
@ -396,33 +409,16 @@ func (h *Headscale) keepAlive(
keepAliveChan <- *data keepAliveChan <- *data
case <-updateCheckerTicker.C: case <-updateCheckerTicker.C:
err := h.UpdateMachine(&m) // Send an update request regardless of outdated or not, if data is sent
// to the node is determined in the updateChan consumer block
n, _ := m.toNode()
err := h.requestUpdate(n)
if err != nil { if err != nil {
log.Error(). log.Error().
Str("func", "keepAlive"). Str("func", "keepAlive").
Str("machine", m.Name). Str("machine", m.Name).
Err(err). Err(err).
Msg("Could not refresh machine details from database") Msgf("Failed to send update request to %s", m.Name)
return
}
if h.isOutdated(&m) {
log.Debug().
Str("func", "keepAlive").
Str("machine", m.Name).
Time("last_successful_update", *m.LastSuccessfulUpdate).
Time("last_state_change", h.getLastStateChange()).
Msgf("There has been updates since the last successful update to %s", m.Name)
// TODO Error checking
n, _ := m.toNode()
h.requestUpdate(n)
} else {
log.Trace().
Str("func", "keepAlive").
Str("machine", m.Name).
Time("last_successful_update", *m.LastSuccessfulUpdate).
Time("last_state_change", h.getLastStateChange()).
Msgf("%s is up to date", m.Name)
} }
} }
} }