Skip to content

Commit

Permalink
update lastseen in db and mapper
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Sep 27, 2023
1 parent 01b85e5 commit 9082450
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
25 changes: 24 additions & 1 deletion hscontrol/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var debugDumpMapResponsePath = envknob.String("HEADSCALE_DEBUG_DUMP_MAPRESPONSE_
// - Keep information about the previous mapresponse so we can send a diff
// - Store hashes
// - Create a "minifier" that removes info not needed for the node
// - some sort of batching, wait for 5 or 60 seconds before sending

type Mapper struct {
privateKey2019 *key.MachinePrivate
Expand Down Expand Up @@ -329,11 +330,28 @@ func (m *Mapper) PeerChangedResponse(
return nil, err
}

// resp.PeerSeenChange = lastSeen
resp.PeerSeenChange = lastSeen

return m.marshalMapResponse(mapRequest, &resp, node, mapRequest.Compress)
}

// PeerOnlineChanged is a no response function that internally updates
// the mappers internal node map with LastSeen and Online information.
func (m *Mapper) PeerOnlineChanged(
mapRequest tailcfg.MapRequest,
node *types.Node,
changed types.Nodes,
pol *policy.ACLPolicy,
) {
m.mu.Lock()
defer m.mu.Unlock()

// Update our internal map.
for _, node := range changed {
m.peers[node.ID].LastSeen = node.LastSeen
}
}

func (m *Mapper) PeerRemovedResponse(
mapRequest tailcfg.MapRequest,
node *types.Node,
Expand Down Expand Up @@ -617,5 +635,10 @@ func appendPeerChanges(
// TODO(kradalby): This currently does not take last seen in keepalives into account
resp.OnlineChange = peers.OnlineNodeMap()

log.Trace().
Interface("OnlineMap", resp.OnlineChange).
Str("hostname", node.Hostname).
Msg("node online map")

return nil
}
50 changes: 24 additions & 26 deletions hscontrol/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,7 @@ func (h *Headscale) handlePoll(
// One alternative is to split these different channels into
// goroutines, but then you might have a problem without a lock
// if a keepalive is written at the same time as an update.
go func() {
err = h.db.UpdateLastSeen(node)
if err != nil {
logErr(err, "Cannot update node LastSeen")

return
}
}()
go h.updateOnline(node)

case update := <-updateChan:
logInfo("Received update")
Expand All @@ -299,6 +292,10 @@ func (h *Headscale) handlePoll(
case types.StateFullUpdate:
logInfo("Sending Full MapResponse")
data, err = mapp.FullMapResponse(mapRequest, node, h.ACLPolicy)
case types.StatePeerOnlineChanged:
logInfo("Sending PeerOnlineChanged MapResponse")
mapp.PeerOnlineChanged(mapRequest, node, update.Changed, h.ACLPolicy)
continue

Check failure on line 298 in hscontrol/poll.go

View workflow job for this annotation

GitHub Actions / golangci-lint

continue with no blank line before (nlreturn)
}

if err != nil {
Expand All @@ -325,16 +322,6 @@ func (h *Headscale) handlePoll(
return
}

// See comment in keepAliveTicker
go func() {
err = h.db.UpdateLastSeen(node)
if err != nil {
logErr(err, "Cannot update node LastSeen")

return
}
}()

log.Info().
Caller().
Bool("noise", isNoise).
Expand All @@ -348,14 +335,7 @@ func (h *Headscale) handlePoll(
case <-ctx.Done():
logInfo("The client has closed the connection")

go func() {
err = h.db.UpdateLastSeen(node)
if err != nil {
logErr(err, "Cannot update node LastSeen")

return
}
}()
go h.updateOnline(node)

// The connection has been closed, so we can stop polling.
return
Expand All @@ -368,6 +348,24 @@ func (h *Headscale) handlePoll(
}
}

func (h *Headscale) updateOnline(node *types.Node) {
now := time.Now()

node.LastSeen = &now

h.nodeNotifier.NotifyWithIgnore(types.StateUpdate{
Type: types.StatePeerOnlineChanged,
Changed: types.Nodes{node},
}, node.MachineKey)

err := h.db.UpdateLastSeen(node)
if err != nil {
log.Error().Err(err).Msg("Cannot update node LastSeen")

return
}
}

func closeChanWithLog[C chan []byte | chan struct{} | chan types.StateUpdate](channel C, node, name string) {
log.Trace().
Str("handler", "PollNetMap").
Expand Down
1 change: 1 addition & 0 deletions hscontrol/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ const (
StatePeerChanged
StatePeerRemoved
StateDERPUpdated
StatePeerOnlineChanged
)

// StateUpdate is an internal message containing information about
Expand Down

0 comments on commit 9082450

Please sign in to comment.