forked from lenonqing/boomer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_gomq.go
177 lines (156 loc) · 3.96 KB
/
client_gomq.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
//go:build !goczmq
// +build !goczmq
package boomer
import (
"fmt"
"github.com/sandwich-go/boost/retry"
"github.com/sandwich-go/boost/xsync"
"log"
"sync"
"github.com/myzhan/gomq"
"github.com/myzhan/gomq/zmtp"
)
type gomqSocketClient struct {
masterHost string
masterPort int
identity string
dealerSocket gomq.Dealer
fromMaster chan message
toMaster chan message
disconnectedFromMaster chan bool
shutdownChan chan bool
state xsync.AtomicInt32
wg sync.WaitGroup
}
func newClient(masterHost string, masterPort int, identity string) (client *gomqSocketClient) {
log.Println("Boomer is built with gomq support.")
client = &gomqSocketClient{
masterHost: masterHost,
masterPort: masterPort,
identity: identity,
fromMaster: make(chan message, 100),
toMaster: make(chan message, 100),
disconnectedFromMaster: make(chan bool),
}
return client
}
func (c *gomqSocketClient) connect() (err error) {
c.shutdownChan = make(chan bool)
addr := fmt.Sprintf("tcp://%s:%d", c.masterHost, c.masterPort)
c.dealerSocket = gomq.NewDealer(zmtp.NewSecurityNull(), c.identity)
c.state.Set(1)
if err = c.dealerSocket.Connect(addr); err != nil {
log.Println("Error connecting to master:", err)
return err
}
log.Printf("Boomer is connected to master(%s) press Ctrl+c to quit.\n", addr)
c.wg.Add(2)
go c.recv()
go c.send()
return nil
}
func (c *gomqSocketClient) reconnect() error {
c.close()
return retry.Do(
func(attempt uint) error {
return c.connect()
},
retry.WithOnRetry(func(attempt uint, err error) {
log.Printf("Attempt %d to reconnect to master: %s\n", attempt, err)
}),
)
}
func (c *gomqSocketClient) close() {
if c.state.CompareAndSwap(1, 0) {
// run一次关一次
close(c.shutdownChan)
if c.dealerSocket != nil {
c.dealerSocket.Close()
}
}
c.wg.Wait() // 等待 send 和 recv 退出
}
func (c *gomqSocketClient) recvChannel() chan message {
return c.fromMaster
}
func (c *gomqSocketClient) recv() {
defer c.wg.Done()
for {
select {
case <-c.shutdownChan:
return
case msg := <-c.dealerSocket.RecvChannel():
if msg.MessageType == zmtp.CommandMessage {
continue
}
if len(msg.Body) == 0 {
continue
}
body, err := msg.Body[0], msg.Err
if err != nil {
log.Printf("Error reading: %v, attempting to reconnect...\n", err)
go func() {
if err := c.reconnect(); err != nil {
log.Printf("Reconnection failed: %v\n", err)
}
}()
continue
}
decodedMsg, err := newGenericMessageFromBytes(body)
if err != nil {
log.Printf("Msgpack decode fail: %v\n", err)
continue
}
if decodedMsg.NodeID != c.identity {
log.Printf("Recv a %s message for node(%s), not for me(%s), dropped.\n", decodedMsg.Type, decodedMsg.NodeID, c.identity)
continue
}
c.fromMaster <- decodedMsg
}
}
}
func (c *gomqSocketClient) sendChannel() chan message {
return c.toMaster
}
func (c *gomqSocketClient) send() {
defer c.wg.Done()
for {
select {
case <-c.shutdownChan:
return
case msg := <-c.toMaster:
err := c.sendMessage(msg)
if err != nil {
log.Printf("Error sending: %v, attempting to reconnect...\n", err)
go func() {
if err := c.reconnect(); err != nil {
log.Printf("Reconnection failed: %v\n", err)
}
}()
continue
}
// We may send genericMessage or clientReadyMessage to master.
m, ok := msg.(*genericMessage)
if ok {
if m.Type == "quit" {
c.disconnectedFromMaster <- true
}
}
}
}
}
func (c *gomqSocketClient) sendMessage(msg message) error {
serializedMessage, err := msg.serialize()
if err != nil {
log.Printf("Msgpack encode fail: %v\n", err)
return err
}
err = c.dealerSocket.Send(serializedMessage)
if err != nil {
log.Printf("Error sending: %v\n", err)
}
return err
}
func (c *gomqSocketClient) disconnectedChannel() chan bool {
return c.disconnectedFromMaster
}