Skip to content

Commit

Permalink
Move isOutdated logic to updateChan consumation
Browse files Browse the repository at this point in the history
  • Loading branch information
kradalby committed Aug 19, 2021
1 parent dd8c0d1 commit 8d1adaa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
5 changes: 5 additions & 0 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
46 changes: 21 additions & 25 deletions poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit 8d1adaa

Please sign in to comment.