2023-06-21 09:29:52 +00:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2023-06-29 10:20:22 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-06-21 09:29:52 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2023-07-24 06:58:51 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-11-19 21:37:04 +00:00
|
|
|
"tailscale.com/types/key"
|
2023-06-21 09:29:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Notifier struct {
|
2023-09-11 11:08:44 +00:00
|
|
|
l sync.RWMutex
|
2023-06-29 10:20:22 +00:00
|
|
|
nodes map[string]chan<- types.StateUpdate
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNotifier() *Notifier {
|
|
|
|
return &Notifier{}
|
|
|
|
}
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
func (n *Notifier) AddNode(machineKey key.MachinePublic, c chan<- types.StateUpdate) {
|
|
|
|
log.Trace().Caller().Str("key", machineKey.ShortString()).Msg("acquiring lock to add node")
|
|
|
|
defer log.Trace().Caller().Str("key", machineKey.ShortString()).Msg("releasing lock to add node")
|
2023-09-11 11:08:44 +00:00
|
|
|
|
2023-06-21 09:29:52 +00:00
|
|
|
n.l.Lock()
|
|
|
|
defer n.l.Unlock()
|
|
|
|
|
|
|
|
if n.nodes == nil {
|
2023-06-29 10:20:22 +00:00
|
|
|
n.nodes = make(map[string]chan<- types.StateUpdate)
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
n.nodes[machineKey.String()] = c
|
2023-07-24 06:58:51 +00:00
|
|
|
|
|
|
|
log.Trace().
|
2023-11-19 21:37:04 +00:00
|
|
|
Str("machine_key", machineKey.ShortString()).
|
2023-07-24 06:58:51 +00:00
|
|
|
Int("open_chans", len(n.nodes)).
|
|
|
|
Msg("Added new channel")
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
func (n *Notifier) RemoveNode(machineKey key.MachinePublic) {
|
|
|
|
log.Trace().Caller().Str("key", machineKey.ShortString()).Msg("acquiring lock to remove node")
|
|
|
|
defer log.Trace().Caller().Str("key", machineKey.ShortString()).Msg("releasing lock to remove node")
|
2023-09-11 11:08:44 +00:00
|
|
|
|
2023-06-21 09:29:52 +00:00
|
|
|
n.l.Lock()
|
|
|
|
defer n.l.Unlock()
|
|
|
|
|
|
|
|
if n.nodes == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-19 21:37:04 +00:00
|
|
|
delete(n.nodes, machineKey.String())
|
2023-07-24 06:58:51 +00:00
|
|
|
|
|
|
|
log.Trace().
|
2023-11-19 21:37:04 +00:00
|
|
|
Str("machine_key", machineKey.ShortString()).
|
2023-07-24 06:58:51 +00:00
|
|
|
Int("open_chans", len(n.nodes)).
|
|
|
|
Msg("Removed channel")
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 10:20:22 +00:00
|
|
|
func (n *Notifier) NotifyAll(update types.StateUpdate) {
|
|
|
|
n.NotifyWithIgnore(update)
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 10:20:22 +00:00
|
|
|
func (n *Notifier) NotifyWithIgnore(update types.StateUpdate, ignore ...string) {
|
2023-09-11 11:08:44 +00:00
|
|
|
log.Trace().Caller().Interface("type", update.Type).Msg("acquiring lock to notify")
|
|
|
|
defer log.Trace().
|
|
|
|
Caller().
|
|
|
|
Interface("type", update.Type).
|
|
|
|
Msg("releasing lock, finished notifing")
|
|
|
|
|
|
|
|
n.l.RLock()
|
|
|
|
defer n.l.RUnlock()
|
2023-06-21 09:29:52 +00:00
|
|
|
|
|
|
|
for key, c := range n.nodes {
|
|
|
|
if util.IsStringInSlice(ignore, key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-09-11 11:08:44 +00:00
|
|
|
log.Trace().Caller().Str("machine", key).Strs("ignoring", ignore).Msg("sending update")
|
2023-06-29 10:20:22 +00:00
|
|
|
c <- update
|
2023-06-21 09:29:52 +00:00
|
|
|
}
|
|
|
|
}
|