/
subscriber.go
107 lines (95 loc) · 1.98 KB
/
subscriber.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
package rabbitmq
import (
"github.com/streadway/amqp"
"github.com/tx7do/kratos-transport/broker"
"sync"
"time"
)
const (
defaultMinResubscribeDelay = 100 * time.Millisecond
defaultMaxResubscribeDelay = 30 * time.Second
defaultExpFactor = time.Duration(2)
defaultResubscribeDelay = defaultMinResubscribeDelay
)
type subscriber struct {
mtx sync.Mutex
mayRun bool
opts broker.SubscribeOptions
topic string
ch *rabbitChannel
durableQueue bool
autoDelete bool
queueArgs map[string]interface{}
r *rabbitBroker
fn func(msg amqp.Delivery)
headers map[string]interface{}
}
func (s *subscriber) Options() broker.SubscribeOptions {
return s.opts
}
func (s *subscriber) Topic() string {
return s.topic
}
func (s *subscriber) Unsubscribe() error {
s.mtx.Lock()
defer s.mtx.Unlock()
s.mayRun = false
if s.ch != nil {
return s.ch.Close()
}
return nil
}
func (s *subscriber) resubscribe() {
minResubscribeDelay := defaultMinResubscribeDelay
maxResubscribeDelay := defaultMaxResubscribeDelay
expFactor := defaultExpFactor
reSubscribeDelay := defaultResubscribeDelay
for {
s.mtx.Lock()
mayRun := s.mayRun
s.mtx.Unlock()
if !mayRun {
// we are unsubscribed, showdown routine
return
}
select {
case <-s.r.conn.close:
return
case <-s.r.conn.waitConnection:
}
s.r.mtx.Lock()
if !s.r.conn.connected {
s.r.mtx.Unlock()
continue
}
ch, sub, err := s.r.conn.Consume(
s.opts.Queue,
s.topic,
s.headers,
s.queueArgs,
s.opts.AutoAck,
s.durableQueue,
s.autoDelete,
)
s.r.mtx.Unlock()
switch err {
case nil:
reSubscribeDelay = minResubscribeDelay
s.mtx.Lock()
s.ch = ch
s.mtx.Unlock()
default:
if reSubscribeDelay > maxResubscribeDelay {
reSubscribeDelay = maxResubscribeDelay
}
time.Sleep(reSubscribeDelay)
reSubscribeDelay *= expFactor
continue
}
for d := range sub {
s.r.wg.Add(1)
s.fn(d)
s.r.wg.Done()
}
}
}