-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
191 lines (159 loc) · 3.27 KB
/
client.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package amqp
import (
"time"
"github.com/streadway/amqp"
"fmt"
"sync"
)
type (
// Client interface which has Call method
Client interface {
Call(endpoint string, message Message) (*Message, error)
}
// Publisher interface which has Publish method
Publisher interface {
Publish(message Message) error
}
// ClientConfig struct
ClientConfig struct {
RequestX string
ResponseX string
ResponseQ string
}
client struct {
sess *session
rec *amqp.Channel
sen *amqp.Channel
requestX string
responseX string
responseQ string
rpcChannelsMu sync.RWMutex
rpcChannels map[string]chan Message
close chan bool
}
)
func (clt *client) run() error {
queue, err := clt.rec.QueueDeclare(
clt.responseQ,
false,
true,
false,
false,
nil,
)
if err != nil {
clt.sess.log.Warn("QueueDeclare", err)
return err
}
if err := clt.rec.QueueBind(
queue.Name,
queue.Name,
clt.responseX,
false,
nil,
); err != nil {
clt.sess.log.Warn("QueueBind", err)
return err
}
msgs, err := clt.rec.Consume(
queue.Name,
"",
true,
false,
false,
false,
nil,
)
if err != nil {
clt.sess.log.Warn("Consume", err)
return err
}
go func(msgs <-chan amqp.Delivery) {
for {
select {
case d := <-msgs:
clt.rpcChannelsMu.RLock()
if c, ok := clt.rpcChannels[d.CorrelationId]; ok {
c <- deliveryToMessage(d)
}
clt.rpcChannelsMu.RUnlock()
case <-clt.close:
fmt.Println("cleanup here?")
clt.cleanup()
}
}
}(msgs)
return nil
}
func (clt *client) stop() {
clt.close <- true
}
func (clt *client) Publish(message Message) error {
message.Timestamp = time.Now()
clt.sess.log.Debug(" -> ", message)
return clt.sen.Publish(
message.Exchange,
message.RoutingKey,
message.Mandatory,
message.Immediate,
message.publishing(),
)
}
func (clt *client) Call(endpoint string, message Message) (*Message, error) {
replyCh := make(chan Message)
correlationId := correlationId(32)
clt.rpcChannelsMu.Lock()
clt.rpcChannels[correlationId] = replyCh
clt.rpcChannelsMu.Unlock()
defer clt.closeReplyChannel(correlationId)
message.Exchange = clt.requestX
message.RoutingKey = endpoint
message.CorrelationId = correlationId
message.ReplyTo = clt.responseQ
message.MessageId = correlationId
message.ContentType = "application/json"
message.Mandatory = false
message.Immediate = false
if err := clt.Publish(message); err != nil {
return nil, err
}
select {
case reply := <-replyCh:
clt.sess.log.Debug(" <- ", reply)
return &reply, nil
case <-time.After(10 * time.Second):
return nil, ErrRpcTimeout
}
return nil, ErrUnknown
}
// Some helper methods
func (clt *client) closeReplyChannel(chName string) {
clt.rpcChannelsMu.Lock()
defer clt.rpcChannelsMu.Unlock()
if ch, ok := clt.rpcChannels[chName]; ok {
close(ch)
delete(clt.rpcChannels, chName)
}
}
func (clt *client) cleanup() error {
clt.sess.log.Info("queue cleanup")
if err := clt.rec.QueueUnbind(
clt.responseQ,
clt.responseQ,
clt.responseX,
nil,
); err != nil {
clt.sess.log.Warn("QueueUnbind", err)
return err
}
clt.rpcChannelsMu.Lock()
for k, ch := range clt.rpcChannels {
close(ch)
delete(clt.rpcChannels, k)
}
clt.rpcChannelsMu.Unlock()
if err := clt.sen.Close(); err != nil {
return err
}
return clt.rec.Close()
}