/
messagetopic.go
124 lines (109 loc) · 3.19 KB
/
messagetopic.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package ppmq
import (
"log"
"sync"
"time"
)
// Init MessageTopic initializing underlying storage engine based on StorageFactory passed
func (mq *MessageTopic) Init(factory StorageFactory) error {
mq.engine = &EngineImpl{}
mq.lock = &sync.Mutex{}
return mq.engine.Init(*mq, factory)
}
// SendToSubscriber sends messages if available to subscriber
func (mq *MessageTopic) SendToSubscriber(cqName string) {
available, err := mq.engine.IsAvailable(cqName)
if err != nil {
log.Println(err)
return
}
if !available {
log.Println("Nope, nothing to push")
return //nothing in the queue or still waiting for ack
}
messages, err := mq.engine.NextBatch(cqName, App.Conf.subscriberMap[cqName].BatchSize)
if err != nil {
log.Println("Error getting next batch", err)
return
}
if len(messages) == 0 {
return // nothing to do
}
log.Println("Pushing bunch of them: ", messages)
Send(cqName, MQWrapper{Topic: mq.Name, Messages: messages})
}
// SendToSubscribers sends messages to all subscribers for give MessageTopic
func (mq *MessageTopic) SendToSubscribers() {
log.Println("Sending messages to customers")
for _, cqName := range mq.engine.GetNamesOfSubscribers() {
mq.SendToSubscriber(cqName)
}
}
// Publish messages to the MessageTopic. The call will add messages to the queue
// and trigger SendToSubscribers call
func (mq *MessageTopic) Publish(msgs []Message) error {
mq.lock.Lock()
defer mq.lock.Unlock()
if err := mq.engine.Add(msgs); err != nil {
return err
}
mq.SendToSubscribers()
return nil
}
// Ack messages for subscriber. The call will trigger SendToSubscriber for the
// given subscriber
func (mq *MessageTopic) Ack(cqName string, ids []MessageKey) error {
mq.lock.Lock()
defer mq.lock.Unlock()
if err := mq.engine.Ack(cqName, ids); err != nil {
return err
}
mq.SendToSubscriber(cqName)
return nil
}
// Reconnect acknowledges MessageTopic that subscriber has reconnected,
// and that whatever messages waiting in Ack should be resent to subscriber
func (mq *MessageTopic) Reconnect(cqName string) error {
mq.lock.Lock()
defer mq.lock.Unlock()
if err := mq.engine.ResetAck(cqName); err != nil {
return err
}
mq.SendToSubscriber(cqName)
return nil
}
// TimeoutAck checks if any of the Ack in the MessageTopic has expired
// and corresponding messages should be resent to subscribers.
// This method will be called automatically once a minute
func (mq *MessageTopic) TimeoutAck() error {
mq.lock.Lock()
defer mq.lock.Unlock()
if err := mq.engine.TimeoutAck(); err != nil {
return err
}
mq.SendToSubscribers()
return nil
}
// ackTimeoutCheck holds the interval on how often TimeoutAck() should be called
var ackTimeoutCheck = 1 * time.Minute
// InitTimeoutAck initializes goroutine that checks for Ack timeouts
func InitTimoutAck(done <-chan bool) {
timer := App.Clock.Timer(ackTimeoutCheck)
// we are using timer/resetting it so that if it takes more than 1min to reset ack
// next one would be in 1 min again
go func() {
for {
select {
case <-done:
return
case <-timer.C:
for _, mq := range App.Conf.topicMap {
if err := mq.TimeoutAck(); err != nil {
log.Printf("Error timeout ack %v", err)
}
}
timer.Reset(ackTimeoutCheck)
}
}
}()
}