pomerium/internal/signal/signal.go
Cuong Manh Le a7bd2caae9
pkg/storage: introduce storage.Backend Watch method (#1135)
Currently, we're doing "sync" in databroker server. If we're going to
support multiple databroker servers instance, this mechanism won't work.

This commit moves the "sync" to storage backend, by adding new Watch
method. The Watch method will return a channel for the caller. Everytime
something happens inside the storage, we notify the caller by sending a
message to this channel.
2020-07-27 21:10:47 +07:00

48 lines
1 KiB
Go

// Package signal provides mechanism for notifying multiple listeners when something happened.
package signal
import (
"sync"
)
// A Signal is used to let multiple listeners know when something happened.
type Signal struct {
mu sync.Mutex
chs map[chan struct{}]struct{}
}
// New creates a new Signal.
func New() *Signal {
return &Signal{
chs: make(map[chan struct{}]struct{}),
}
}
// Broadcast signals all the listeners. Broadcast never blocks.
func (s *Signal) Broadcast() {
s.mu.Lock()
for ch := range s.chs {
select {
case ch <- struct{}{}:
default:
}
}
s.mu.Unlock()
}
// Bind creates a new listening channel bound to the signal. The channel used has a size of 1
// and any given broadcast will signal at least one event, but may signal more than one.
func (s *Signal) Bind() chan struct{} {
ch := make(chan struct{}, 1)
s.mu.Lock()
s.chs[ch] = struct{}{}
s.mu.Unlock()
return ch
}
// Unbind stops the listening channel bound to the signal.
func (s *Signal) Unbind(ch chan struct{}) {
s.mu.Lock()
delete(s.chs, ch)
s.mu.Unlock()
}