mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-31 18:07:17 +02:00
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.
This commit is contained in:
parent
d9711c8055
commit
a7bd2caae9
10 changed files with 204 additions and 44 deletions
48
internal/signal/signal.go
Normal file
48
internal/signal/signal.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
// 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()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue