Skip to content

Commit 4e0a57e

Browse files
committed
Mod: delete gxlog
1 parent 96c2170 commit 4e0a57e

23 files changed

+3014
-152
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176

177177
END OF TERMS AND CONDITIONS
178178

179-
Copyright 2016 ~ 2017 Alex Stocks.
179+
Copyright 2016 ~ 2018 Alex Stocks.
180180

181181
Licensed under the Apache License, Version 2.0 (the "License");
182182
you may not use this file except in compliance with the License.

README.md

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
# getty #
2-
---
1+
# getty
2+
33
*a netty like asynchronous network I/O library*
44

5-
## introdction ##
6-
---
5+
## INTRO
76

87
Getty is a asynchronous network I/O library in golang. Getty is based on "ngo" whose author is [sanbit](https://github.com/sanbit). Getty works on tcp/udp/websocket network protocol and supplies [a uniform interface](https://github.com/alexstocks/getty/blob/master/getty.go#L45).
98

@@ -15,7 +14,29 @@ Whatever if you use websocket, you do not need to care about hearbeat request/re
1514

1615
You can get code example in https://github.com/AlexStocks/getty-examples.
1716

18-
## LICENCE ##
19-
---
17+
## RPC
18+
19+
A open source, Go based, RPC framework.
20+
21+
Feature list:
22+
23+
- 1 Transport: TCP(√), UDP, Websocket
24+
- 2 Codec: ProtoBuf(√), JSON(√)
25+
- 3 Service Discovery: Service Publish(X), Service Watch(X), Service Notify(X)
26+
- 4 Registry: ZooKeeper(X), Etcd(x)
27+
- 5 Strategy: Failover(√), Failfast(√)
28+
- 6 Load Balance: Random(X), RoundRobin(X)
29+
- 7 Metrics: Invoke Statistics(x), User Auth(x)
30+
31+
Code example:
32+
33+
The rpc dir of [getty-examples](https://github.com/alexstocks/getty-examples/) shows how to build rpc client/rpc server.
34+
35+
##
36+
37+
38+
39+
## LICENCE
40+
2041
Apache License 2.0
2142

change_log.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,38 @@
1414
## develop history ##
1515
---
1616

17+
- 2018/08/07
18+
> Improvement
19+
* RPC package format: {2 Bytes Header len + Header + 2 Body len + Body} ---> {Header + Body}
20+
> Bug Fix
21+
* do not encode body if package body is nil
22+
23+
- 2018/07/01
24+
> Feature
25+
* Add RPC
26+
27+
- 2018/06/25
28+
> buf fix
29+
* Using juju/errors.Cause on read/write in case of network i/o timeout
30+
31+
- 2018/03/29
32+
> improvement
33+
* use juju/errors instead of pkg/errors
34+
35+
- 2018/03/20
36+
> bug fix
37+
* ignore connectPingPackage
38+
39+
- 2018/03/19
40+
> improvement
41+
* use gxnet.IsSameAddr
42+
* send out pkg asap in WritePkg when the second parameter @timeout is not greater then 0.
43+
* delete Chinese commenting
44+
* gettyConn:readCount -> gettyConn:readBytes
45+
* gettyConn:writeCount -> gettyConn:writeBytes
46+
* gettyConn:readPkgCount -> gettyConn:readPkgNum
47+
* gettyConn:writePkgCount -> gettyConn:writePkgNum
48+
1749
- 2018/03/18
1850
> improvement
1951
* nerr -> netError
@@ -22,6 +54,8 @@
2254
* close net.UDPConn when connected failed
2355
* close net.Conn when connected failed
2456
* Session::EndPointType() -> Session::EndPoint()
57+
* time.Sleep() -> wheel.After()
58+
* do not check server.go:server::caCert
2559

2660
- 2018/03/17
2761
> improvement
@@ -287,9 +321,9 @@
287321
- 2016/08/29
288322
> 1 rename reconnect to errFlag in function session.go:(Session)handlePackage
289323
>
290-
> 2 session.go:(gettyConn)readCount is reconsidered as read in tcp stream bytes
324+
> 2 session.go:(gettyConn)readBytes is reconsidered as read in tcp stream bytes
291325
>
292-
> 3 session.go:(gettyConn)writeCount is reconsidered as write out tcp stream bytes
326+
> 3 session.go:(gettyConn)writeBytes is reconsidered as write out tcp stream bytes
293327
>
294328
> 4 reconstruct session output token string session.go:(Session)sessionToken
295329
>

client.go

+43-24
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@ import (
2222
)
2323

2424
import (
25+
"github.com/AlexStocks/goext/net"
2526
"github.com/AlexStocks/goext/sync"
2627
log "github.com/AlexStocks/log4go"
2728
"github.com/gorilla/websocket"
29+
jerrors "github.com/juju/errors"
2830
)
2931

3032
const (
3133
connInterval = 3e9 // 3s
3234
connectTimeout = 5e9
3335
maxTimes = 10
34-
pingPacket = "ping"
36+
)
37+
38+
var (
39+
connectPingPackage = []byte("connect-ping")
3540
)
3641

3742
/////////////////////////////////////////
@@ -126,16 +131,17 @@ func (c *client) dialTCP() Session {
126131
return nil
127132
}
128133
conn, err = net.DialTimeout("tcp", c.addr, connectTimeout)
129-
if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() {
134+
if err == nil && gxnet.IsSameAddr(conn.RemoteAddr(), conn.LocalAddr()) {
130135
conn.Close()
131136
err = errSelfConnect
132137
}
133138
if err == nil {
134139
return newTCPSession(conn, c)
135140
}
136141

137-
log.Info("net.DialTimeout(addr:%s, timeout:%v) = error{%s}", c.addr, err)
138-
time.Sleep(connInterval)
142+
log.Info("net.DialTimeout(addr:%s, timeout:%v) = error{%s}", c.addr, jerrors.ErrorStack(err))
143+
// time.Sleep(connInterval)
144+
<-wheel.After(connInterval)
139145
}
140146
}
141147

@@ -157,34 +163,36 @@ func (c *client) dialUDP() Session {
157163
return nil
158164
}
159165
conn, err = net.DialUDP("udp", localAddr, peerAddr)
160-
if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() {
166+
if err == nil && gxnet.IsSameAddr(conn.RemoteAddr(), conn.LocalAddr()) {
161167
conn.Close()
162168
err = errSelfConnect
163169
}
164170
if err != nil {
165-
log.Warn("net.DialTimeout(addr:%s, timeout:%v) = error{%s}", c.addr, err)
166-
time.Sleep(connInterval)
171+
log.Warn("net.DialTimeout(addr:%s, timeout:%v) = error{%s}", c.addr, jerrors.ErrorStack(err))
172+
// time.Sleep(connInterval)
173+
<-wheel.After(connInterval)
167174
continue
168175
}
169176

170177
// check connection alive by write/read action
171-
copy(buf, []byte(pingPacket))
172178
conn.SetWriteDeadline(wheel.Now().Add(1e9))
173-
if length, err = conn.Write(buf[:len(pingPacket)]); err != nil {
179+
if length, err = conn.Write(connectPingPackage[:]); err != nil {
174180
conn.Close()
175-
log.Warn("conn.Write(%s) = {length:%d, err:%s}", pingPacket, length, err)
176-
time.Sleep(connInterval)
181+
log.Warn("conn.Write(%s) = {length:%d, err:%s}", string(connectPingPackage), length, jerrors.ErrorStack(err))
182+
// time.Sleep(connInterval)
183+
<-wheel.After(connInterval)
177184
continue
178185
}
179186
conn.SetReadDeadline(wheel.Now().Add(1e9))
180187
length, err = conn.Read(buf)
181-
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
188+
if netErr, ok := jerrors.Cause(err).(net.Error); ok && netErr.Timeout() {
182189
err = nil
183190
}
184191
if err != nil {
185-
log.Info("conn{%#v}.Read() = {length:%d, err:%s}", conn, length, err)
192+
log.Info("conn{%#v}.Read() = {length:%d, err:%s}", conn, length, jerrors.ErrorStack(err))
186193
conn.Close()
187-
time.Sleep(connInterval)
194+
// time.Sleep(connInterval)
195+
<-wheel.After(connInterval)
188196
continue
189197
}
190198
//if err == nil {
@@ -207,8 +215,8 @@ func (c *client) dialWS() Session {
207215
return nil
208216
}
209217
conn, _, err = dialer.Dial(c.addr, nil)
210-
log.Info("websocket.dialer.Dial(addr:%s) = error:%s", c.addr, err)
211-
if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() {
218+
log.Info("websocket.dialer.Dial(addr:%s) = error:%s", c.addr, jerrors.ErrorStack(err))
219+
if err == nil && gxnet.IsSameAddr(conn.RemoteAddr(), conn.LocalAddr()) {
212220
conn.Close()
213221
err = errSelfConnect
214222
}
@@ -221,8 +229,9 @@ func (c *client) dialWS() Session {
221229
return ss
222230
}
223231

224-
log.Info("websocket.dialer.Dial(addr:%s) = error:%s", c.addr, err)
225-
time.Sleep(connInterval)
232+
log.Info("websocket.dialer.Dial(addr:%s) = error:%s", c.addr, jerrors.ErrorStack(err))
233+
// time.Sleep(connInterval)
234+
<-wheel.After(connInterval)
226235
}
227236
}
228237

@@ -247,7 +256,7 @@ func (c *client) dialWSS() Session {
247256
if c.cert != "" {
248257
certPEMBlock, err := ioutil.ReadFile(c.cert)
249258
if err != nil {
250-
panic(fmt.Sprintf("ioutil.ReadFile(cert:%s) = error{%s}", c.cert, err))
259+
panic(fmt.Sprintf("ioutil.ReadFile(cert:%s) = error{%s}", c.cert, jerrors.ErrorStack(err)))
251260
}
252261

253262
var cert tls.Certificate
@@ -269,7 +278,7 @@ func (c *client) dialWSS() Session {
269278
for _, c := range config.Certificates {
270279
roots, err = x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
271280
if err != nil {
272-
panic(fmt.Sprintf("error parsing server's root cert: %s\n", err))
281+
panic(fmt.Sprintf("error parsing server's root cert: %s\n", jerrors.ErrorStack(err)))
273282
}
274283
for _, root = range roots {
275284
certPool.AddCert(root)
@@ -285,7 +294,7 @@ func (c *client) dialWSS() Session {
285294
return nil
286295
}
287296
conn, _, err = dialer.Dial(c.addr, nil)
288-
if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() {
297+
if err == nil && gxnet.IsSameAddr(conn.RemoteAddr(), conn.LocalAddr()) {
289298
conn.Close()
290299
err = errSelfConnect
291300
}
@@ -299,8 +308,9 @@ func (c *client) dialWSS() Session {
299308
return ss
300309
}
301310

302-
log.Info("websocket.dialer.Dial(addr:%s) = error{%s}", c.addr, err)
303-
time.Sleep(connInterval)
311+
log.Info("websocket.dialer.Dial(addr:%s) = error{%s}", c.addr, jerrors.ErrorStack(err))
312+
// time.Sleep(connInterval)
313+
<-wheel.After(connInterval)
304314
}
305315
}
306316

@@ -361,11 +371,19 @@ func (c *client) connect() {
361371
}
362372
}
363373

374+
// there are two methods to keep connection pool. the first approch is like
375+
// redigo's lazy connection pool(https://github.com/gomodule/redigo/blob/master/redis/pool.go:),
376+
// in which you should apply testOnBorrow to check alive of the connection.
377+
// the second way is as follows. @RunEventLoop detects the aliveness of the connection
378+
// in regular time interval.
379+
// the active method maybe overburden the cpu slightly.
380+
// however, you can get a active tcp connection very quickly.
364381
func (c *client) RunEventLoop(newSession NewSessionCallback) {
365382
c.Lock()
366383
c.newSession = newSession
367384
c.Unlock()
368385

386+
log.Info("run")
369387
c.wg.Add(1)
370388
// a for-loop goroutine to make sure the connection is valid
371389
go func() {
@@ -389,7 +407,8 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) {
389407
if maxTimes < times {
390408
times = maxTimes
391409
}
392-
time.Sleep(time.Duration(int64(times) * connInterval))
410+
// time.Sleep(time.Duration(int64(times) * connInterval))
411+
<-wheel.After(time.Duration(int64(times) * connInterval))
393412
continue
394413
}
395414
times = 0

0 commit comments

Comments
 (0)