-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacker.go
63 lines (54 loc) · 1.25 KB
/
acker.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
package final
import (
"context"
"github.com/sirupsen/logrus"
)
// acker 启动 Options.NumAcker 个goroutine接收消息队列ack消息后,Done掉 outbox 中的消息记录
type acker struct {
logger *logrus.Entry
// 退出信号
exit chan bool
id string
bus *Bus
}
func newAcker(id string, bus *Bus) *acker {
s := &acker{
logger: bus.logger.WithFields(logrus.Fields{
"module": "acker",
"acker_id": id,
}),
exit: make(chan bool),
id: id,
bus: bus,
}
return s
}
func (acker *acker) Start(ctx context.Context) error {
acker.logger.Info("Acker start success")
go func() {
for {
select {
case <-ctx.Done():
acker.logger.Info("Acker stop success")
return
case ack, ok := <-acker.bus.publisher.ack:
if !ok {
acker.logger.Error("acker.ack close")
return
}
err := acker.bus.publisher.confirm(ack)
if err != nil {
acker.logger.WithError(err).Error("acker confirm error")
}
case nack, ok := <-acker.bus.publisher.nack:
if !ok {
acker.logger.Error("acker.nack close")
return
}
acker.logger.WithField("nack", nack).Error("nack received")
acker.logger.WithField("channel_len", len(acker.bus.publisher.nack)).Debug("length of nack channel")
}
}
}()
return nil
}