forked from Meituan-Dianping/cat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
144 lines (123 loc) · 3.28 KB
/
sender.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
package cat
import (
"bytes"
"encoding/binary"
"net"
"time"
"github.com/Meituan-Dianping/cat-go/message"
)
func createHeader() *message.Header {
return &message.Header{
Domain: config.domain,
Hostname: config.hostname,
Ip: config.ip,
MessageId: Manager.NextId(),
ParentMessageId: "",
RootMessageId: "",
}
}
type catMessageSender struct {
scheduleMixin
normal chan message.Messager
high chan message.Messager
chConn chan net.Conn
encoder message.Encoder
buf *bytes.Buffer
conn net.Conn
}
func (s *catMessageSender) GetName() string {
return "Sender"
}
func (s *catMessageSender) send(m message.Messager) {
var buf = s.buf
buf.Reset()
var header = createHeader()
if err := s.encoder.EncodeHeader(buf, header); err != nil {
return
}
if err := s.encoder.EncodeMessage(buf, m); err != nil {
return
}
var b = make([]byte, 4)
binary.BigEndian.PutUint32(b, uint32(buf.Len()))
if err := s.conn.SetWriteDeadline(time.Now().Add(time.Second * 3)); err != nil {
logger.Warning("Error occurred while setting write deadline, connection has been dropped.")
s.conn = nil
router.signals <- signalResetConnection
}
if _, err := s.conn.Write(b); err != nil {
logger.Warning("Error occurred while writing data, connection has been dropped.")
s.conn = nil
router.signals <- signalResetConnection
return
}
if _, err := s.conn.Write(buf.Bytes()); err != nil {
logger.Warning("Error occurred while writing data, connection has been dropped.")
s.conn = nil
router.signals <- signalResetConnection
return
}
return
}
func (s *catMessageSender) handleTransaction(trans *message.Transaction) {
if trans.GetStatus() != SUCCESS {
select {
case s.high <- trans:
default:
logger.Warning("High priority channel is full, transaction has been discarded.")
}
} else {
select {
case s.normal <- trans:
default:
// logger.Warning("Normal priority channel is full, transaction has been discarded.")
}
}
}
func (s *catMessageSender) handleEvent(event *message.Event) {
select {
case s.normal <- event:
default:
// logger.Warning("Normal priority channel is full, event has been discarded.")
}
}
func (s *catMessageSender) beforeStop() {
close(s.chConn)
close(s.high)
close(s.normal)
for m := range s.high {
s.send(m)
}
for m := range s.normal {
s.send(m)
}
}
func (s *catMessageSender) process() {
if s.conn == nil {
s.conn = <-s.chConn
logger.Info("Received a new connection: %s", s.conn.RemoteAddr().String())
return
}
select {
case sig := <-s.signals:
s.handle(sig)
case conn := <-s.chConn:
logger.Info("Received a new connection: %s", conn.RemoteAddr().String())
s.conn = conn
case m := <-s.high:
// logger.Debug("Receive a message [%s|%s] from high priority channel", m.GetType(), m.GetName())
s.send(m)
case m := <-s.normal:
// logger.Debug("Receive a message [%s|%s] from normal priority channel", m.GetType(), m.GetName())
s.send(m)
}
}
var sender = catMessageSender{
scheduleMixin: makeScheduleMixedIn(signalSenderExit),
normal: make(chan message.Messager, normalPriorityQueueSize),
high: make(chan message.Messager, highPriorityQueueSize),
chConn: make(chan net.Conn),
encoder: message.NewBinaryEncoder(),
buf: bytes.NewBuffer([]byte{}),
conn: nil,
}