52 lines
766 B
Go
52 lines
766 B
Go
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)
|
|
}
|
|
}
|
|
}
|
|
}
|