Merge pull request #82 from juanfont/really-expire-ephemeral

Also notify peers when deleting ephemerals
This commit is contained in:
Juan Font 2021-08-12 22:05:53 +02:00 committed by GitHub
commit 5bfcf5c917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 21 deletions

9
app.go
View File

@ -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://") {

View File

@ -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)

View File

@ -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
}

View File

@ -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
}
}
}