mirror of
https://github.com/juanfont/headscale.git
synced 2025-01-19 10:20:05 +09:00
Merge pull request #82 from juanfont/really-expire-ephemeral
Also notify peers when deleting ephemerals
This commit is contained in:
commit
5bfcf5c917
4 changed files with 34 additions and 21 deletions
9
app.go
9
app.go
|
@ -107,9 +107,9 @@ func (h *Headscale) redirect(w http.ResponseWriter, req *http.Request) {
|
||||||
http.Redirect(w, req, target, http.StatusFound)
|
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
|
// 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)
|
ticker := time.NewTicker(time.Duration(milliSeconds) * time.Millisecond)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
h.expireEphemeralNodesWorker()
|
h.expireEphemeralNodesWorker()
|
||||||
|
@ -135,6 +135,10 @@ func (h *Headscale) expireEphemeralNodesWorker() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Str("machine", m.Name).Msg("🤮 Cannot delete ephemeral machine from the database")
|
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
|
var err error
|
||||||
|
|
||||||
go h.watchForKVUpdates(5000)
|
go h.watchForKVUpdates(5000)
|
||||||
|
go h.expireEphemeralNodes(5000)
|
||||||
|
|
||||||
if h.cfg.TLSLetsEncryptHostname != "" {
|
if h.cfg.TLSLetsEncryptHostname != "" {
|
||||||
if !strings.HasPrefix(h.cfg.ServerURL, "https://") {
|
if !strings.HasPrefix(h.cfg.ServerURL, "https://") {
|
||||||
|
|
|
@ -21,7 +21,7 @@ var serveCmd = &cobra.Command{
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error initializing: %s", err)
|
log.Fatalf("Error initializing: %s", err)
|
||||||
}
|
}
|
||||||
go h.ExpireEphemeralNodes(5000)
|
|
||||||
err = h.Serve()
|
err = h.Serve()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error initializing: %s", err)
|
log.Fatalf("Error initializing: %s", err)
|
||||||
|
|
23
machine.go
23
machine.go
|
@ -238,3 +238,26 @@ func (m *Machine) GetHostInfo() (*tailcfg.Hostinfo, error) {
|
||||||
}
|
}
|
||||||
return &hostinfo, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -169,24 +169,9 @@ func (h *Headscale) checkForNamespacesPendingUpdates() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, m := range *machines {
|
for _, m := range *machines {
|
||||||
peers, _ := h.getPeers(m)
|
err = h.notifyChangesToPeers(&m)
|
||||||
for _, p := range *peers {
|
if err != nil {
|
||||||
pUp, ok := h.clientsPolling.Load(uint64(p.ID))
|
continue
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue