-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpubsub.go
98 lines (90 loc) · 2.47 KB
/
pubsub.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"sync"
"time"
)
type subscriber struct {
feed chan update
subsCount int32
}
type subscribersMap = map[int64]uint64
type vkSource struct {
lastPost int64
subs subscribersMap
}
type pubsub struct {
pubToSub map[int64]vkSource // vk group id to a list of subscriber ids
subscribers map[int64]subscriber // tg channel id to it's vk feed and subCount
mu sync.RWMutex
}
func (ps *pubsub) subscribe(sub int64, pub int64, flags uint64, consumer func(<-chan update)) {
ps.mu.Lock()
defer ps.mu.Unlock()
if _, exists := ps.subscribers[sub]; !exists {
ps.subscribers[sub] = subscriber{feed: make(chan update, 4), subsCount: 0}
go consumer(ps.subscribers[sub].feed)
}
s := ps.subscribers[sub]
s.subsCount++
ps.subscribers[sub] = s
if _, exists := ps.pubToSub[pub]; !exists {
ps.pubToSub[pub] = vkSource{lastPost: time.Now().Unix(), subs: make(subscribersMap)}
}
ps.pubToSub[pub].subs[sub] = flags
}
func (ps *pubsub) addSubscriber(sub int64, consumer func(<-chan update)) {
if _, exists := ps.subscribers[sub]; !exists {
ps.subscribers[sub] = subscriber{feed: make(chan update, 4)}
go consumer(ps.subscribers[sub].feed)
}
}
func (ps *pubsub) addPublisher(pub int64, pubInstnce vkSource) {
if _, exists := ps.pubToSub[pub]; !exists {
ps.pubToSub[pub] = pubInstnce
}
}
func (ps *pubsub) subscribeSimple(sub int64, pub int64, flags uint64) {
s := ps.subscribers[sub]
s.subsCount++
ps.subscribers[sub] = s
ps.pubToSub[pub].subs[sub] = flags
}
func (ps *pubsub) updateTimeStamp(pubID int64, lastPost int64) {
ps.mu.Lock()
defer ps.mu.Unlock()
pub := ps.pubToSub[pubID]
pub.lastPost = lastPost
ps.pubToSub[pubID] = pub
}
func (ps *pubsub) unsubscribe(sub int64, pub int64) {
ps.mu.Lock()
defer ps.mu.Unlock()
delete(ps.pubToSub[pub].subs, sub)
// log.Printf("Deleted subscruber %d from publisher %d\n", sub, pub)
if len(ps.pubToSub[pub].subs) == 0 {
// log.Printf("Deleted publisher %d\n", pub)
delete(ps.pubToSub, pub)
}
s := ps.subscribers[sub]
s.subsCount--
ps.subscribers[sub] = s
if s.subsCount == 0 {
close(ps.subscribers[sub].feed)
delete(ps.subscribers, sub)
// log.Printf("Deleted subscriber %d\n", sub)
}
}
func (ps *pubsub) publish(pub int64, msg []preparedPost) {
ps.mu.Lock()
for sub, flags := range ps.pubToSub[pub].subs {
ps.subscribers[sub].feed <- update{msg, flags}
}
ps.mu.Unlock()
}
func (ps *pubsub) stopPubSub() {
ps.mu.Lock()
for _, sub := range ps.subscribers {
close(sub.feed)
}
ps.mu.Unlock()
}