pomerium/internal/databroker/signal.go
Cuong Manh Le 2f84dd2aff
Add storage backend interface (#1072)
* pkg: add storage package

Which contains storage.Backend interface to initial support for multiple
backend storage.

* pkg/storage: add inmemory storage

* internal/databroker: use storage.Backend interface

Instead of implementing multiple databroker server implementation for
each kind of storage backend, we use only one databroker server
implementation, which is supported multiple storage backends, which
satisfy storage.Backend interface.
2020-07-15 09:42:01 +07:00

45 lines
985 B
Go

package databroker
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{}
}
// NewSignal creates a new Signal.
func NewSignal() *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()
}