Skip to content

Commit

Permalink
fix resetting expiry every tick
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
  • Loading branch information
kradalby committed Mar 7, 2024
1 parent 5251277 commit 4724b6c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 27 deletions.
3 changes: 2 additions & 1 deletion hscontrol/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ func (h *Headscale) expireExpiredMachines(intervalMs int64) {
continue
}

log.Trace().Interface("nodes", update.ChangeNodes).Msgf("expiring nodes")
if changed {
log.Trace().Interface("nodes", update.ChangePatches).Msgf("expiring nodes")

ctx := types.NotifyCtx(context.Background(), "expire-expired", "na")
h.nodeNotifier.NotifyAll(ctx, update)
}
Expand Down
27 changes: 2 additions & 25 deletions hscontrol/db/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,35 +749,12 @@ func ExpireExpiredNodes(tx *gorm.DB,

return time.Unix(0, 0), types.StateUpdate{}, false
}
for index, node := range nodes {
if node.IsExpired() &&
// TODO(kradalby): Replace this, it is very spammy
// It will notify about all nodes that has been expired.
// It should only notify about expired nodes since _last check_.
node.Expiry.After(lastCheck) {
for _, node := range nodes {
if node.IsExpired() && node.Expiry.After(lastCheck) {
expired = append(expired, &tailcfg.PeerChange{
NodeID: tailcfg.NodeID(node.ID),
KeyExpiry: node.Expiry,
})

now := time.Now()
// Do not use setNodeExpiry as that has a notifier hook, which
// can cause a deadlock, we are updating all changed nodes later
// and there is no point in notifiying twice.
if err := tx.Model(&nodes[index]).Updates(types.Node{
Expiry: &now,
}).Error; err != nil {
log.Error().
Err(err).
Str("node", node.Hostname).
Str("name", node.GivenName).
Msg("🤮 Cannot expire node")
} else {
log.Info().
Str("node", node.Hostname).
Str("name", node.GivenName).
Msg("Node successfully expired")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion hscontrol/types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (node Node) IsExpired() bool {
return false
}

return time.Now().UTC().After(*node.Expiry)
return time.Since(*node.Expiry) > 0
}

// IsEphemeral returns if the node is registered as an Ephemeral node.
Expand Down

0 comments on commit 4724b6c

Please sign in to comment.