mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-22 23:25:26 +01:00
45 lines
837 B
Go
45 lines
837 B
Go
package service
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/getAlby/lndhub.go/db/models"
|
|
)
|
|
|
|
type Pubsub struct {
|
|
mu sync.RWMutex
|
|
subs map[int64]map[string]chan models.Invoice
|
|
}
|
|
|
|
func NewPubsub() *Pubsub {
|
|
ps := &Pubsub{}
|
|
ps.subs = make(map[int64]map[string]chan models.Invoice)
|
|
return ps
|
|
}
|
|
|
|
func (ps *Pubsub) Subscribe(id string, topic int64, ch chan models.Invoice) {
|
|
ps.mu.Lock()
|
|
defer ps.mu.Unlock()
|
|
if ps.subs[topic] == nil {
|
|
ps.subs[topic] = make(map[string]chan models.Invoice)
|
|
}
|
|
ps.subs[topic][id] = ch
|
|
}
|
|
|
|
func (ps *Pubsub) Unsubscribe(id string, topic int64) error {
|
|
ps.mu.Lock()
|
|
defer ps.mu.Unlock()
|
|
close(ps.subs[topic][id])
|
|
delete(ps.subs[topic], id)
|
|
return nil
|
|
}
|
|
|
|
func (ps *Pubsub) Publish(topic int64, msg models.Invoice) {
|
|
ps.mu.RLock()
|
|
defer ps.mu.RUnlock()
|
|
|
|
for _, ch := range ps.subs[topic] {
|
|
ch <- msg
|
|
}
|
|
}
|