From 8d1adaaef3abac891f6794c63268b5ac47c747e2 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 19 Aug 2021 18:05:33 +0100 Subject: [PATCH] Move isOutdated logic to updateChan consumation --- machine.go | 5 +++++ poll.go | 46 +++++++++++++++++++++------------------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/machine.go b/machine.go index 14efd008..13e3529a 100644 --- a/machine.go +++ b/machine.go @@ -315,6 +315,11 @@ func (h *Headscale) requestUpdate(m *tailcfg.Node) error { } func (h *Headscale) isOutdated(m *Machine) bool { + err := h.UpdateMachine(m) + if err != nil { + return true + } + lastChange := h.getLastStateChange() log.Trace(). Str("func", "keepAlive"). diff --git a/poll.go b/poll.go index d58d45f1..27358fc5 100644 --- a/poll.go +++ b/poll.go @@ -300,12 +300,18 @@ func (h *Headscale) PollNetMapStream( return true case <-updateChan: + log.Trace(). + Str("handler", "PollNetMapStream"). + Str("machine", m.Name). + Str("channel", "update"). + Msg("Received a request for update") if h.isOutdated(&m) { - log.Trace(). + log.Debug(). Str("handler", "PollNetMapStream"). Str("machine", m.Name). - Str("channel", "update"). - Msg("Received a request for update") + 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) data, err := h.getMapResponse(mKey, req, m) if err != nil { log.Error(). @@ -337,6 +343,13 @@ func (h *Headscale) PollNetMapStream( now := time.Now().UTC() m.LastSuccessfulUpdate = &now 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 @@ -396,33 +409,16 @@ func (h *Headscale) keepAlive( keepAliveChan <- *data 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 { log.Error(). Str("func", "keepAlive"). Str("machine", m.Name). Err(err). - Msg("Could not refresh machine details from database") - 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) + Msgf("Failed to send update request to %s", m.Name) } } }