-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
258 lines (222 loc) · 6.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package net4g
import (
"github.com/carsonsx/log4g"
"net"
"os"
"os/signal"
"runtime"
"sync"
"time"
"github.com/carsonsx/gutil"
)
const (
reconnect_delay_min = 100
reconnect_delay_max = 10000
)
func NewTcpClient(name string, addrFn func() (addrs []*NetAddr, err error)) *TCPClient {
client := new(TCPClient)
client.name = name
client.addrFn = addrFn
client.AutoReconnect = true
client.reconnectDelay = reconnect_delay_min
client.sig = make(chan os.Signal, 2)
return client
}
type TCPClient struct {
name string
addrFn func() (addrs []*NetAddr, err error)
readIntercepter Intercepter
writeIntercepter Intercepter
AutoReconnect bool
reconnectDelay int
serializer Serializer
dispatchers []*Dispatcher
hub NetHub
heartbeat bool
heartbeatData []byte
sig chan os.Signal
closed bool
firstConnected sync.WaitGroup
monitor bool
monitorLog *log4g.Loggers
startTime time.Time
}
func (c *TCPClient) SetSerializer(serializer Serializer) *TCPClient {
c.serializer = serializer
return c
}
func (c *TCPClient) AddDispatchers(dispatchers ...*Dispatcher) *TCPClient {
for _, d := range dispatchers {
c.dispatchers = append(c.dispatchers, d)
}
return c
}
func (c *TCPClient) SetReadIntercepter(intercepter Intercepter) *TCPClient {
c.readIntercepter = intercepter
return c
}
func (c *TCPClient) SetWriteIntercepter(intercepter Intercepter) *TCPClient {
c.writeIntercepter = intercepter
return c
}
func (c *TCPClient) SetHub(hub NetHub) *TCPClient {
c.hub = hub
return c
}
func (c *TCPClient) EnableMonitor(monitorLog *log4g.Loggers) *TCPClient {
c.monitor = true
c.monitorLog = monitorLog
if c.monitorLog == nil {
c.monitorLog = log4g.NewLoggers()
}
return c
}
func (c *TCPClient) DisableAutoReconnect() *TCPClient {
c.AutoReconnect = false
return c
}
func (c *TCPClient) Connect() *TCPClient {
if c.hub == nil {
c.hub = NewNetHub(false, true)
}
c.hub.SetSerializer(c.serializer)
for _, d := range c.dispatchers {
d.serializer = c.serializer
}
addrs, err := c.addrFn()
if err != nil {
panic(err)
}
c.firstConnected.Add(len(addrs))
for _, addr := range addrs {
go c.doConnect(addr)
}
// for first connection
c.firstConnected.Wait()
if c.heartbeat {
timer := time.NewTicker(NetConfig.HeartbeatFrequency * time.Second)
c.heartbeatData = NetConfig.HeartbeatData
go func() {
for {
if c.hub.Closed() {
break
}
log4g.Trace("[client] heart beat...")
c.hub.BroadcastAll(c.heartbeatData)
<-timer.C
}
}()
}
c.startTime = time.Now()
if c.monitor {
go func() {
ticker := time.NewTicker(time.Duration(NetConfig.MonitorBeat) * time.Second)
previousTime := c.startTime
for {
if c.closed {
ticker.Stop()
break
}
select {
case <-ticker.C:
c.hub.Statistics()
trc, twc, prc, pwc := c.hub.PackCount()
now := time.Now()
duration := now.Sub(previousTime)
previousTime = now
c.monitorLog.Info("")
c.monitorLog.Info("*[%s status] goroutine: %d, connection: %d", c.name, runtime.NumGoroutine(), c.hub.Count())
c.monitorLog.Info("*[%s message] read: %d, write: %d", c.name, trc, twc)
c.monitorLog.Info("*[%s msg/sec] read: %d, write: %d", c.name, prc*int64(time.Second)/int64(duration), pwc*int64(time.Second)/int64(duration))
trd, twd, ord, owd := c.hub.DataUsage()
c.monitorLog.Info("*[%s data usage] read: %s, write: %s", c.name, gutil.HumanReadableByteCount(trd*int64(time.Second)/int64(duration), true), gutil.HumanReadableByteCount(twd*int64(time.Second)/int64(duration), true))
//s.monitorLog.Info("*[%s data offset] read: %s, write: %s", s.Name, gutil.HumanReadableByteCount(ord, true), gutil.HumanReadableByteCount(owd, true))
c.monitorLog.Info("*[%s data/sec] read: %s, write: %s", c.name, gutil.HumanReadableByteCount(ord*int64(time.Second)/int64(duration), true), gutil.HumanReadableByteCount(owd*int64(time.Second)/int64(duration), true))
}
}
}()
}
return c
}
func (c *TCPClient) doConnect(addr *NetAddr) {
var connected bool
for {
if conn, err := c.connect(addr.Key, addr.Addr); err == nil {
if !connected {
c.firstConnected.Done()
connected = true
}
newNetReader(conn, c.serializer, c.dispatchers, c.hub).Read(func(data []byte) bool {
if IsHeartbeatData(data) {
log4g.Trace("heartbeat from server")
c.hub.Heartbeat(conn)
return false
}
return true
})
c.hub.Remove(conn)
agent := newNetAgent(c.hub, conn, nil, nil, nil, c.serializer)
for _, d := range c.dispatchers {
d.dispatchConnectionClosedEvent(agent)
}
}
if c.closed || !c.AutoReconnect {
log4g.Info("disconnected")
c.sig <- Terminal
break
}
log4g.Info("delay %d millisecond to reconnect", c.reconnectDelay)
time.Sleep(time.Duration(c.reconnectDelay) * time.Millisecond)
c.reconnectDelay *= 2
if c.reconnectDelay > reconnect_delay_max {
c.reconnectDelay = reconnect_delay_max
}
}
}
func (c *TCPClient) connect(name, addr string) (conn NetConn, err error) {
var netconn net.Conn
netconn, err = net.Dial("tcp", addr)
if err != nil {
log4g.Error(err)
return
}
log4g.Info("connected to %s", netconn.RemoteAddr().String())
c.reconnectDelay = reconnect_delay_min
conn = newTcpConn(netconn, c.readIntercepter, c.writeIntercepter)
_conn := c.hub.Get(name)
if _conn != nil {
failedData := _conn.NotWrittenData()
if len(failedData) > 0 {
log4g.Info("found %d failed write data, will rewrite by new connection", len(failedData))
}
for _, data := range failedData {
conn.Write(data)
}
}
c.hub.Add(name, conn)
agent := newNetAgent(c.hub, conn, nil, nil, nil, c.serializer)
for _, d := range c.dispatchers {
d.dispatchConnectionCreatedEvent(agent)
}
return
}
func (c *TCPClient) Close() {
c.closed = true
//close all connections
//c.hub.CloseConnections()
//log4g.Info("closed client[%s] connections", c.name)
//close net hub
c.hub.Destroy()
log4g.Info("closed client[%s] hub", c.name)
//close dispatchers
for _, d := range c.dispatchers {
d.Destroy()
}
log4g.Info("closed client[%s] dispatcher", c.name)
log4g.Info("closed client[%s]", c.name)
}
func (c *TCPClient) Wait() {
signal.Notify(c.sig, os.Interrupt, os.Kill, Terminal)
log4g.Info("client[%s] is closing with signal %v\n", c.name, <-c.sig)
c.Close()
}