Adding approval process

This commit is contained in:
2023-07-03 17:26:43 -04:00
parent a43264189f
commit b1b010deee
15 changed files with 640 additions and 71 deletions

51
pkg/pubsub/pubsub.go Normal file
View File

@@ -0,0 +1,51 @@
package pubsub
import "sync"
type Pubsub struct {
mu sync.RWMutex
subs map[string][]chan string
closed bool
}
func New() *Pubsub {
ps := Pubsub{}
ps.subs = make(map[string][]chan string)
return &ps
}
func (ps *Pubsub) Publish(topic string, msg string) {
ps.mu.RLock()
defer ps.mu.RUnlock()
if ps.closed {
return
}
for _, ch := range ps.subs[topic] {
ch <- msg
}
}
func (ps *Pubsub) Subscribe(topic string) <-chan string {
ps.mu.Lock()
defer ps.mu.Unlock()
ch := make(chan string, 1)
ps.subs[topic] = append(ps.subs[topic], ch)
return ch
}
func (ps *Pubsub) Close() {
ps.mu.Lock()
defer ps.mu.Unlock()
if !ps.closed {
ps.closed = true
for _, subs := range ps.subs {
for _, ch := range subs {
close(ch)
}
}
}
}