Add some conifguration and logging for the hub

This commit is contained in:
2023-07-04 22:05:24 -04:00
parent f82d3f18c6
commit 31f6cc0a0d
18 changed files with 250 additions and 151 deletions

51
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)
}
}
}
}