diff --git a/app.go b/app.go index 91605c03..34fb77b1 100644 --- a/app.go +++ b/app.go @@ -107,9 +107,9 @@ func (h *Headscale) redirect(w http.ResponseWriter, req *http.Request) { http.Redirect(w, req, target, http.StatusFound) } -// ExpireEphemeralNodes deletes ephemeral machine records that have not been +// expireEphemeralNodes deletes ephemeral machine records that have not been // seen for longer than h.cfg.EphemeralNodeInactivityTimeout -func (h *Headscale) ExpireEphemeralNodes(milliSeconds int64) { +func (h *Headscale) expireEphemeralNodes(milliSeconds int64) { ticker := time.NewTicker(time.Duration(milliSeconds) * time.Millisecond) for range ticker.C { h.expireEphemeralNodesWorker() @@ -135,6 +135,10 @@ func (h *Headscale) expireEphemeralNodesWorker() { if err != nil { log.Error().Err(err).Str("machine", m.Name).Msg("🤮 Cannot delete ephemeral machine from the database") } + err = h.notifyChangesToPeers(&m) + if err != nil { + continue + } } } } @@ -165,6 +169,7 @@ func (h *Headscale) Serve() error { var err error go h.watchForKVUpdates(5000) + go h.expireEphemeralNodes(5000) if h.cfg.TLSLetsEncryptHostname != "" { if !strings.HasPrefix(h.cfg.ServerURL, "https://") { diff --git a/cmd/headscale/cli/server.go b/cmd/headscale/cli/server.go index 1f8db6a2..6d9ad194 100644 --- a/cmd/headscale/cli/server.go +++ b/cmd/headscale/cli/server.go @@ -21,7 +21,7 @@ var serveCmd = &cobra.Command{ if err != nil { log.Fatalf("Error initializing: %s", err) } - go h.ExpireEphemeralNodes(5000) + err = h.Serve() if err != nil { log.Fatalf("Error initializing: %s", err) diff --git a/machine.go b/machine.go index 4c7e5945..81208a83 100644 --- a/machine.go +++ b/machine.go @@ -238,3 +238,26 @@ func (m *Machine) GetHostInfo() (*tailcfg.Hostinfo, error) { } return &hostinfo, nil } + +func (h *Headscale) notifyChangesToPeers(m *Machine) error { + peers, _ := h.getPeers(*m) + for _, p := range *peers { + pUp, ok := h.clientsPolling.Load(uint64(p.ID)) + if ok { + log.Info(). + Str("func", "notifyChangesToPeers"). + Str("machine", m.Name). + Str("peer", m.Name). + Str("address", p.Addresses[0].String()). + Msgf("Notifying peer %s (%s)", p.Name, p.Addresses[0]) + pUp.(chan []byte) <- []byte{} + } else { + log.Info(). + Str("func", "notifyChangesToPeers"). + Str("machine", m.Name). + Str("peer", m.Name). + Msgf("Peer %s does not appear to be polling", p.Name) + } + } + return nil +} diff --git a/namespaces.go b/namespaces.go index 9b8d1904..1bf8c2df 100644 --- a/namespaces.go +++ b/namespaces.go @@ -169,24 +169,9 @@ func (h *Headscale) checkForNamespacesPendingUpdates() { continue } for _, m := range *machines { - peers, _ := h.getPeers(m) - for _, p := range *peers { - pUp, ok := h.clientsPolling.Load(uint64(p.ID)) - if ok { - log.Info(). - Str("func", "checkForNamespacesPendingUpdates"). - Str("machine", m.Name). - Str("peer", m.Name). - Str("address", p.Addresses[0].String()). - Msgf("Notifying peer %s (%s)", p.Name, p.Addresses[0]) - pUp.(chan []byte) <- []byte{} - } else { - log.Info(). - Str("func", "checkForNamespacesPendingUpdates"). - Str("machine", m.Name). - Str("peer", m.Name). - Msgf("Peer %s does not appear to be polling", p.Name) - } + err = h.notifyChangesToPeers(&m) + if err != nil { + continue } } }