-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
136 lines (130 loc) · 3.03 KB
/
publisher.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
125
126
127
128
129
130
131
132
133
134
135
136
package piper
import (
"context"
"encoding/json"
"errors"
amqp "github.com/rabbitmq/amqp091-go"
"log"
"sync"
"time"
)
type Publisher struct {
conn *Connection
config PublisherConfig
isConnected bool
isClose bool
name string
muConn sync.RWMutex
muClose sync.RWMutex
messages chan any
reportMessages chan Report
done chan bool
}
func NewPublisher(config PublisherConfig, ch *Connection) (*Publisher, error) {
if ch == nil {
return nil, errors.New("connection is not defined")
}
publisher := &Publisher{
config: config,
messages: make(chan any),
reportMessages: make(chan Report),
conn: ch,
name: config.Exchange + "_" + config.RoutingKey,
done: make(chan bool),
}
return publisher, nil
}
func (p *Publisher) Connect() error {
p.muConn.Lock()
defer p.muConn.Unlock()
if p.isConnected {
return nil
}
if err := p.conn.ExchangeDeclare(
p.config.Exchange,
p.config.ExchangeKind,
true,
false,
false,
false,
nil); err != nil {
return err
}
p.isConnected = true
return nil
}
func (p *Publisher) IsClose() bool {
p.muClose.RLock()
defer p.muClose.RUnlock()
return p.isClose
}
func (p *Publisher) Publish(ctx context.Context) chan any {
p.muConn.RLock()
defer p.muConn.RUnlock()
if !p.isConnected {
for {
if err := p.Connect(); err != nil {
log.Printf("[ERROR] [AMQP PUBLISHER] error declare exchange(%s) %s", p.config.Exchange, p.config.RoutingKey)
time.Sleep(10 * time.Second)
continue
}
log.Printf("[AMQP PUBLISHER] is connected (%s) %s", p.config.Exchange, p.config.RoutingKey)
break
}
}
return p.messages
}
func (p *Publisher) Done() chan bool {
return p.done
}
func (p *Publisher) Stop(ctx context.Context) error {
log.Printf("[AMQP PUBLISHER] STOP (%s) %s", p.config.Exchange, p.config.RoutingKey)
close(p.reportMessages)
close(p.messages)
p.muClose.Lock()
p.isClose = true
p.muClose.Unlock()
return nil
}
func (p *Publisher) Start(ctx context.Context) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case payload, ok := <-p.messages:
if !ok {
if p.conn.IsClosed() {
log.Printf("[AMQP PUBLISHER] channel is closed (%s) %s", p.config.Exchange, p.config.RoutingKey)
p.muClose.Lock()
p.isClose = true
p.muClose.Unlock()
}
return
}
if !p.isConnected {
continue
}
buffer, err := json.Marshal(payload)
if err != nil {
log.Printf("[ERROR] [AMQP PUBLISHER] (%s) %s failed marshal: (%s) ", p.config.Exchange, p.config.RoutingKey, err)
continue
}
err = p.conn.Publish(p, amqp.Publishing{
Body: buffer,
ContentType: "application/json",
})
if err != nil {
p.muConn.Lock()
p.isConnected = false
p.muConn.Unlock()
log.Printf("[ERROR] [AMQP PUBLISHER] error publish: (%s) %s: %s", p.config.Exchange, p.config.RoutingKey, err)
}
}
}
}()
wg.Wait()
p.Done() <- true
log.Printf("[AMQP PUBLISHER] DONE (%s)", p.name)
}