This moves node ping controller to using the new internal lock API. The reason for this is twofold: * The channel approach that was used to notify other controllers of changes could only be used once (at startup), and couldn't be used in the future to broadcast node ping status. The idea idea is here that we could move to a sync.Cond style API and only wakeup other controllers on change, as opposed to constantly polling each other * The problem with sync.Cond is that it's not context friendly. If we want to do stuff like wait on a sync.cond and use a context or a timer or similar, it doesn't work whereas this API allows context cancellations on condition change. The idea is that as we have more controllers that act as centralized sources of authority, they can broadcast out their state.
99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package lock
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// NewMonitorVariable instantiates an empty monitor variable
|
|
func NewMonitorVariable() MonitorVariable {
|
|
mv := &monitorVariable{
|
|
versionInvalidationChannel: make(chan struct{}),
|
|
}
|
|
return mv
|
|
}
|
|
|
|
// MonitorVariable is a specific monitor variable which allows for channel-subscription to changes to
|
|
// the internal value of the MonitorVariable.
|
|
type MonitorVariable interface {
|
|
Set(value interface{})
|
|
Subscribe() Subscription
|
|
}
|
|
|
|
// Subscription is not concurrency safe. It must not be shared between multiple goroutines.
|
|
type Subscription interface {
|
|
// On instantiation, if the value has been set, this will return a closed channel. Otherwise, it will follow the
|
|
// standard semantic, which is when the Monitor Variable is updated, this channel will close. The channel is updated
|
|
// based on reading Value(). Once a value is read, the channel returned will only be closed if a the Monitor Variable
|
|
// is set to a new value.
|
|
NewValueReady() <-chan struct{}
|
|
// Value returns a value object in a non-blocking fashion. This also means it may return an uninitialized value.
|
|
// If the monitor variable has not yet been set, the "Version" of the value will be 0.
|
|
Value() Value
|
|
}
|
|
|
|
type Value struct {
|
|
Value interface{}
|
|
Version int64
|
|
}
|
|
|
|
type monitorVariable struct {
|
|
lock sync.Mutex
|
|
currentValue interface{}
|
|
// 0 indicates uninitialized
|
|
currentVersion int64
|
|
versionInvalidationChannel chan struct{}
|
|
}
|
|
|
|
func (m *monitorVariable) Set(newValue interface{}) {
|
|
m.lock.Lock()
|
|
defer m.lock.Unlock()
|
|
m.currentValue = newValue
|
|
m.currentVersion++
|
|
close(m.versionInvalidationChannel)
|
|
m.versionInvalidationChannel = make(chan struct{})
|
|
}
|
|
|
|
func (m *monitorVariable) Subscribe() Subscription {
|
|
m.lock.Lock()
|
|
defer m.lock.Unlock()
|
|
sub := &subscription{
|
|
mv: m,
|
|
}
|
|
if m.currentVersion > 0 {
|
|
// A value has been set. Set the first versionInvalidationChannel to a closed one.
|
|
closedCh := make(chan struct{})
|
|
close(closedCh)
|
|
sub.lastVersionReadInvalidationChannel = closedCh
|
|
} else {
|
|
// The value hasn't yet been initialized.
|
|
sub.lastVersionReadInvalidationChannel = m.versionInvalidationChannel
|
|
}
|
|
|
|
return sub
|
|
}
|
|
|
|
type subscription struct {
|
|
mv *monitorVariable
|
|
lastVersionRead int64
|
|
lastVersionReadInvalidationChannel chan struct{}
|
|
}
|
|
|
|
func (s *subscription) NewValueReady() <-chan struct{} {
|
|
/* This lock could be finer grained (on just the subscription) */
|
|
s.mv.lock.Lock()
|
|
defer s.mv.lock.Unlock()
|
|
return s.lastVersionReadInvalidationChannel
|
|
}
|
|
|
|
func (s *subscription) Value() Value {
|
|
s.mv.lock.Lock()
|
|
defer s.mv.lock.Unlock()
|
|
val := Value{
|
|
Value: s.mv.currentValue,
|
|
Version: s.mv.currentVersion,
|
|
}
|
|
s.lastVersionRead = s.mv.currentVersion
|
|
s.lastVersionReadInvalidationChannel = s.mv.versionInvalidationChannel
|
|
return val
|
|
}
|