-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
64 lines (54 loc) · 1.4 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package reportclient
import (
"log"
"sync"
"github.com/ScoreTrak/ScoreTrak/pkg/queue"
"github.com/ScoreTrak/ScoreTrak/pkg/queue/queueing"
"github.com/ScoreTrak/ScoreTrak/pkg/report/reportrepo"
"github.com/gofrs/uuid"
)
type Client struct {
repo reportrepo.Repo
pubsub queue.MasterStreamPubSub
cnf queueing.MasterConfig
signal map[uuid.UUID]chan struct{}
signalMutex *sync.RWMutex
}
func NewReportClient(cnf queueing.MasterConfig, repo reportrepo.Repo, pubsub queue.MasterStreamPubSub) *Client {
return &Client{repo: repo, cnf: cnf, signalMutex: &sync.RWMutex{},
pubsub: pubsub, signal: make(map[uuid.UUID]chan struct{}),
}
}
func (a *Client) Subscribe() (uuid.UUID, <-chan struct{}) {
a.signalMutex.Lock()
defer a.signalMutex.Unlock()
ch := make(chan struct{}, 1)
uid, err := uuid.NewV4()
if err != nil {
log.Panicln("Unable to generate rabdom UUID")
}
a.signal[uid] = ch
return uid, ch
}
func (a *Client) Unsubscribe(uid uuid.UUID) {
a.signalMutex.Lock()
defer a.signalMutex.Unlock()
delete(a.signal, uid)
}
func (a *Client) Publish() {
a.signalMutex.RLock()
defer a.signalMutex.RUnlock()
for _, ch := range a.signal {
ch <- struct{}{}
}
}
func (a *Client) Notify() {
a.pubsub.NotifyTopic(a.cnf.ChannelPrefix + "_report")
}
func (a *Client) ReportClient() {
recvChannel := a.pubsub.ReceiveUpdateFromTopic(a.cnf.ChannelPrefix + "_report")
for {
<-recvChannel
a.Publish()
}
}