-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe_worker.go
92 lines (76 loc) · 2.25 KB
/
subscribe_worker.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
package marina
import (
"sync"
"sync/atomic"
"github.com/TheSmallBoat/cabinet"
"github.com/lithdew/kademlia"
)
const defaultMaxSubscribeWorkers = 4
// The subscribe packets come from the peer-nodes.
type SubscribeWorker struct {
tp *taskPool
twp *TwinsPool
tt *cabinet.TTree
subSucNum uint32 // the success count of the subscribing operation
subErrNum uint32 // the error count of the subscribing operation
unSubSucNum uint32 // the success count of the unsubscribing operation
unSubErrNum uint32 // the error count of the unsubscribing operation
wg sync.WaitGroup
}
func NewSubscribeWorker(twp *TwinsPool, tTree *cabinet.TTree) *SubscribeWorker {
return &SubscribeWorker{
tp: newTaskPool(defaultMaxSubscribeWorkers),
twp: twp,
tt: tTree,
subSucNum: 0,
subErrNum: 0,
unSubSucNum: 0,
unSubErrNum: 0,
}
}
// kid : the subscribe-peer-node kadId
func (s *SubscribeWorker) PeerNodeSubscribe(prd *TwinServiceProvider, qos byte, topic []byte) {
if qos == byte(1) {
// Todo:process response
}
s.wg.Add(1)
s.tp.submitTask(func() { processPeerNodeSubscribe(s, prd, topic) })
}
func (s *SubscribeWorker) PeerNodeUnSubscribe(pubK kademlia.PublicKey, qos byte, topic []byte) {
if qos == byte(1) {
// Todo:process response
}
s.wg.Add(1)
s.tp.submitTask(func() { processPeerNodeUnSubscribe(s, pubK, topic) })
}
// To link the twin for the peer-node to this topic
func processPeerNodeSubscribe(subW *SubscribeWorker, prd *TwinServiceProvider, topic []byte) {
defer subW.wg.Done()
err := subW.tt.EntityLink(topic, subW.twp.acquire(prd))
if err != nil {
atomic.AddUint32(&subW.subErrNum, uint32(1))
} else {
atomic.AddUint32(&subW.subSucNum, uint32(1))
}
}
// To unlink the twin for the peer-node to this topic
func processPeerNodeUnSubscribe(subW *SubscribeWorker, pubK kademlia.PublicKey, topic []byte) {
defer subW.wg.Done()
tw, exist := subW.twp.existTwin(pubK)
if exist {
err := subW.tt.EntityUnLink(topic, tw)
if err != nil {
atomic.AddUint32(&subW.unSubErrNum, uint32(1))
} else {
atomic.AddUint32(&subW.unSubSucNum, uint32(1))
}
} else {
atomic.AddUint32(&subW.unSubErrNum, uint32(1))
}
}
func (s *SubscribeWorker) Close() {
s.tp.close()
}
func (s *SubscribeWorker) Wait() {
s.wg.Wait()
}