Replace the timestamp based state system
This commit replaces the timestamp based state system with a new one that has update channels directly to the connected nodes. It will send an update to all listening clients via the polling mechanism. It introduces a new package notifier, which has a concurrency safe manager for all our channels to the connected nodes. Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
parent
056d3a81c5
commit
66ff1fcd40
13 changed files with 216 additions and 731 deletions
55
hscontrol/notifier/notifier.go
Normal file
55
hscontrol/notifier/notifier.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package notifier
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/juanfont/headscale/hscontrol/util"
|
||||
)
|
||||
|
||||
type Notifier struct {
|
||||
l sync.RWMutex
|
||||
nodes map[string]chan<- struct{}
|
||||
}
|
||||
|
||||
func NewNotifier() *Notifier {
|
||||
return &Notifier{}
|
||||
}
|
||||
|
||||
func (n *Notifier) AddNode(machineKey string, c chan<- struct{}) {
|
||||
n.l.Lock()
|
||||
defer n.l.Unlock()
|
||||
|
||||
if n.nodes == nil {
|
||||
n.nodes = make(map[string]chan<- struct{})
|
||||
}
|
||||
|
||||
n.nodes[machineKey] = c
|
||||
}
|
||||
|
||||
func (n *Notifier) RemoveNode(machineKey string) {
|
||||
n.l.Lock()
|
||||
defer n.l.Unlock()
|
||||
|
||||
if n.nodes == nil {
|
||||
return
|
||||
}
|
||||
|
||||
delete(n.nodes, machineKey)
|
||||
}
|
||||
|
||||
func (n *Notifier) NotifyAll() {
|
||||
n.NotifyWithIgnore()
|
||||
}
|
||||
|
||||
func (n *Notifier) NotifyWithIgnore(ignore ...string) {
|
||||
n.l.RLock()
|
||||
defer n.l.RUnlock()
|
||||
|
||||
for key, c := range n.nodes {
|
||||
if util.IsStringInSlice(ignore, key) {
|
||||
continue
|
||||
}
|
||||
|
||||
c <- struct{}{}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue