mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-24 08:47:49 +00:00
Make lastStateChange namespaced
This commit is contained in:
parent
48ef6e5a6f
commit
b0ec945dbb
40
app.go
40
app.go
@ -60,8 +60,7 @@ type Headscale struct {
|
|||||||
|
|
||||||
clientsUpdateChannels sync.Map
|
clientsUpdateChannels sync.Map
|
||||||
|
|
||||||
lastStateChangeMutex sync.RWMutex
|
lastStateChange sync.Map
|
||||||
lastStateChange time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHeadscale returns the Headscale app
|
// NewHeadscale returns the Headscale app
|
||||||
@ -88,13 +87,12 @@ func NewHeadscale(cfg Config) (*Headscale, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
h := Headscale{
|
h := Headscale{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
dbType: cfg.DBtype,
|
dbType: cfg.DBtype,
|
||||||
dbString: dbString,
|
dbString: dbString,
|
||||||
privateKey: privKey,
|
privateKey: privKey,
|
||||||
publicKey: &pubKey,
|
publicKey: &pubKey,
|
||||||
aclRules: &tailcfg.FilterAllowAll, // default allowall
|
aclRules: &tailcfg.FilterAllowAll, // default allowall
|
||||||
lastStateChange: time.Now().UTC(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.initDB()
|
err = h.initDB()
|
||||||
@ -229,17 +227,19 @@ func (h *Headscale) Serve() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Headscale) setLastStateChangeToNow() {
|
func (h *Headscale) setLastStateChangeToNow(namespace string) {
|
||||||
h.lastStateChangeMutex.Lock()
|
now := time.Now().UTC()
|
||||||
|
h.lastStateChange.Store(namespace, now)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Headscale) getLastStateChange(namespace string) time.Time {
|
||||||
|
if wrapped, ok := h.lastStateChange.Load(namespace); ok {
|
||||||
|
lastChange, _ := wrapped.(time.Time)
|
||||||
|
return lastChange
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
h.lastStateChange = now
|
h.lastStateChange.Store(namespace, now)
|
||||||
|
return now
|
||||||
h.lastStateChangeMutex.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Headscale) getLastStateChange() time.Time {
|
|
||||||
h.lastStateChangeMutex.RLock()
|
|
||||||
defer h.lastStateChangeMutex.RUnlock()
|
|
||||||
return h.lastStateChange
|
|
||||||
}
|
}
|
||||||
|
@ -320,7 +320,7 @@ func (h *Headscale) isOutdated(m *Machine) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
lastChange := h.getLastStateChange()
|
lastChange := h.getLastStateChange(m.Namespace.Name)
|
||||||
log.Trace().
|
log.Trace().
|
||||||
Str("func", "keepAlive").
|
Str("func", "keepAlive").
|
||||||
Str("machine", m.Name).
|
Str("machine", m.Name).
|
||||||
|
6
poll.go
6
poll.go
@ -123,7 +123,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
|
|||||||
|
|
||||||
// There has been an update to _any_ of the nodes that the other nodes would
|
// There has been an update to _any_ of the nodes that the other nodes would
|
||||||
// need to know about
|
// need to know about
|
||||||
h.setLastStateChangeToNow()
|
h.setLastStateChangeToNow(m.Namespace.Name)
|
||||||
|
|
||||||
// The request is not ReadOnly, so we need to set up channels for updating
|
// The request is not ReadOnly, so we need to set up channels for updating
|
||||||
// peers via longpoll
|
// peers via longpoll
|
||||||
@ -310,7 +310,7 @@ func (h *Headscale) PollNetMapStream(
|
|||||||
Str("handler", "PollNetMapStream").
|
Str("handler", "PollNetMapStream").
|
||||||
Str("machine", m.Name).
|
Str("machine", m.Name).
|
||||||
Time("last_successful_update", *m.LastSuccessfulUpdate).
|
Time("last_successful_update", *m.LastSuccessfulUpdate).
|
||||||
Time("last_state_change", h.getLastStateChange()).
|
Time("last_state_change", h.getLastStateChange(m.Namespace.Name)).
|
||||||
Msgf("There has been updates since the last successful update to %s", m.Name)
|
Msgf("There has been updates since the last successful update to %s", m.Name)
|
||||||
data, err := h.getMapResponse(mKey, req, m)
|
data, err := h.getMapResponse(mKey, req, m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -348,7 +348,7 @@ func (h *Headscale) PollNetMapStream(
|
|||||||
Str("handler", "PollNetMapStream").
|
Str("handler", "PollNetMapStream").
|
||||||
Str("machine", m.Name).
|
Str("machine", m.Name).
|
||||||
Time("last_successful_update", *m.LastSuccessfulUpdate).
|
Time("last_successful_update", *m.LastSuccessfulUpdate).
|
||||||
Time("last_state_change", h.getLastStateChange()).
|
Time("last_state_change", h.getLastStateChange(m.Namespace.Name)).
|
||||||
Msgf("%s is up to date", m.Name)
|
Msgf("%s is up to date", m.Name)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user