Unverified Commit d200327c authored by fangyincheng's avatar fangyincheng Committed by GitHub

Merge pull request #2 from u0x01/master

Merge from AlexStocks/getty
parents 533281db 600adac3
...@@ -29,8 +29,8 @@ import ( ...@@ -29,8 +29,8 @@ import (
) )
const ( const (
connInterval = 3e9 // 3s connInterval = 5e8 // 500ms
connectTimeout = 5e9 connectTimeout = 3e9
maxTimes = 10 maxTimes = 10
) )
...@@ -175,7 +175,7 @@ func (c *client) dialUDP() Session { ...@@ -175,7 +175,7 @@ func (c *client) dialUDP() Session {
} }
// check connection alive by write/read action // check connection alive by write/read action
conn.SetWriteDeadline(wheel.Now().Add(1e9)) conn.SetWriteDeadline(time.Now().Add(1e9))
if length, err = conn.Write(connectPingPackage[:]); err != nil { if length, err = conn.Write(connectPingPackage[:]); err != nil {
conn.Close() conn.Close()
log.Warn("conn.Write(%s) = {length:%d, err:%s}", string(connectPingPackage), length, jerrors.ErrorStack(err)) log.Warn("conn.Write(%s) = {length:%d, err:%s}", string(connectPingPackage), length, jerrors.ErrorStack(err))
...@@ -183,7 +183,7 @@ func (c *client) dialUDP() Session { ...@@ -183,7 +183,7 @@ func (c *client) dialUDP() Session {
<-wheel.After(connInterval) <-wheel.After(connInterval)
continue continue
} }
conn.SetReadDeadline(wheel.Now().Add(1e9)) conn.SetReadDeadline(time.Now().Add(1e9))
length, err = conn.Read(buf) length, err = conn.Read(buf)
if netErr, ok := jerrors.Cause(err).(net.Error); ok && netErr.Timeout() { if netErr, ok := jerrors.Cause(err).(net.Error); ok && netErr.Timeout() {
err = nil err = nil
...@@ -361,6 +361,10 @@ func (c *client) connect() { ...@@ -361,6 +361,10 @@ func (c *client) connect() {
// ss.RunEventLoop() // ss.RunEventLoop()
ss.(*session).run() ss.(*session).run()
c.Lock() c.Lock()
if c.ssMap == nil {
c.Unlock()
break
}
c.ssMap[ss] = struct{}{} c.ssMap[ss] = struct{}{}
c.Unlock() c.Unlock()
ss.SetAttribute(sessionClientKey, c) ss.SetAttribute(sessionClientKey, c)
...@@ -408,8 +412,7 @@ func (c *client) reConnect() { ...@@ -408,8 +412,7 @@ func (c *client) reConnect() {
if maxTimes < times { if maxTimes < times {
times = maxTimes times = maxTimes
} }
// time.Sleep(time.Duration(int64(times) * connInterval)) time.Sleep(time.Duration(int64(times) * int64(c.reconnectInterval)))
<-wheel.After(time.Duration(int64(times) * connInterval))
} }
} }
......
...@@ -240,7 +240,7 @@ func (t *gettyTCPConn) read(p []byte) (int, error) { ...@@ -240,7 +240,7 @@ func (t *gettyTCPConn) read(p []byte) (int, error) {
// Optimization: update read deadline only if more than 25% // Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded. // of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details. // See https://github.com/golang/go/issues/15133 for details.
currentTime = wheel.Now() currentTime = time.Now()
if currentTime.Sub(t.rLastDeadline) > (t.rTimeout >> 2) { if currentTime.Sub(t.rLastDeadline) > (t.rTimeout >> 2) {
if err = t.conn.SetReadDeadline(currentTime.Add(t.rTimeout)); err != nil { if err = t.conn.SetReadDeadline(currentTime.Add(t.rTimeout)); err != nil {
return 0, jerrors.Trace(err) return 0, jerrors.Trace(err)
...@@ -273,7 +273,7 @@ func (t *gettyTCPConn) Write(pkg interface{}) (int, error) { ...@@ -273,7 +273,7 @@ func (t *gettyTCPConn) Write(pkg interface{}) (int, error) {
// Optimization: update write deadline only if more than 25% // Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded. // of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details. // See https://github.com/golang/go/issues/15133 for details.
currentTime = wheel.Now() currentTime = time.Now()
if currentTime.Sub(t.wLastDeadline) > (t.wTimeout >> 2) { if currentTime.Sub(t.wLastDeadline) > (t.wTimeout >> 2) {
if err = t.conn.SetWriteDeadline(currentTime.Add(t.wTimeout)); err != nil { if err = t.conn.SetWriteDeadline(currentTime.Add(t.wTimeout)); err != nil {
return 0, jerrors.Trace(err) return 0, jerrors.Trace(err)
...@@ -390,7 +390,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) { ...@@ -390,7 +390,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
// Optimization: update read deadline only if more than 25% // Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded. // of the last read deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details. // See https://github.com/golang/go/issues/15133 for details.
currentTime = wheel.Now() currentTime = time.Now()
if currentTime.Sub(u.rLastDeadline) > (u.rTimeout >> 2) { if currentTime.Sub(u.rLastDeadline) > (u.rTimeout >> 2) {
if err = u.conn.SetReadDeadline(currentTime.Add(u.rTimeout)); err != nil { if err = u.conn.SetReadDeadline(currentTime.Add(u.rTimeout)); err != nil {
return 0, nil, jerrors.Trace(err) return 0, nil, jerrors.Trace(err)
...@@ -438,7 +438,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) { ...@@ -438,7 +438,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
// Optimization: update write deadline only if more than 25% // Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded. // of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details. // See https://github.com/golang/go/issues/15133 for details.
currentTime = wheel.Now() currentTime = time.Now()
if currentTime.Sub(u.wLastDeadline) > (u.wTimeout >> 2) { if currentTime.Sub(u.wLastDeadline) > (u.wTimeout >> 2) {
if err = u.conn.SetWriteDeadline(currentTime.Add(u.wTimeout)); err != nil { if err = u.conn.SetWriteDeadline(currentTime.Add(u.wTimeout)); err != nil {
return 0, jerrors.Trace(err) return 0, jerrors.Trace(err)
...@@ -564,7 +564,7 @@ func (w *gettyWSConn) updateWriteDeadline() error { ...@@ -564,7 +564,7 @@ func (w *gettyWSConn) updateWriteDeadline() error {
// Optimization: update write deadline only if more than 25% // Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded. // of the last write deadline exceeded.
// See https://github.com/golang/go/issues/15133 for details. // See https://github.com/golang/go/issues/15133 for details.
currentTime = wheel.Now() currentTime = time.Now()
if currentTime.Sub(w.wLastDeadline) > (w.wTimeout >> 2) { if currentTime.Sub(w.wLastDeadline) > (w.wTimeout >> 2) {
if err = w.conn.SetWriteDeadline(currentTime.Add(w.wTimeout)); err != nil { if err = w.conn.SetWriteDeadline(currentTime.Add(w.wTimeout)); err != nil {
return jerrors.Trace(err) return jerrors.Trace(err)
......
module github.com/dubbogo/getty module github.com/dubbogo/getty
require ( require (
github.com/AlexStocks/getty v1.0.4
github.com/AlexStocks/goext v0.3.2 github.com/AlexStocks/goext v0.3.2
github.com/AlexStocks/log4go v1.0.2 // indirect github.com/AlexStocks/log4go v1.0.2
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dubbogo/log4go v0.0.0-20190406152735-41c57e1073e9 github.com/dubbogo/log4go v0.0.0-20190406152735-41c57e1073e9
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gogo/protobuf v1.2.1
github.com/golang/snappy v0.0.1 github.com/golang/snappy v0.0.1
github.com/gorilla/websocket v1.4.0 github.com/gorilla/websocket v1.4.0
github.com/juju/errors v0.0.0-20190207033735-e65537c515d7 github.com/juju/errors v0.0.0-20190207033735-e65537c515d7
github.com/juju/loggo v0.0.0-20190212223446-d976af380377 // indirect
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/kr/pretty v0.1.0 // indirect github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
github.com/mattn/go-colorable v0.1.1 // indirect go.etcd.io/etcd v3.3.13+incompatible // indirect
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v2 v2.2.2
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
) )
This diff is collapsed.
...@@ -70,6 +70,7 @@ type ClientOption func(*ClientOptions) ...@@ -70,6 +70,7 @@ type ClientOption func(*ClientOptions)
type ClientOptions struct { type ClientOptions struct {
addr string addr string
number int number int
reconnectInterval int// reConnect Interval
// the cert file of wss server which may contain server domain, server ip, the starting effective date, effective // the cert file of wss server which may contain server domain, server ip, the starting effective date, effective
// duration, the hash alg, the len of the private key. // duration, the hash alg, the len of the private key.
...@@ -84,6 +85,13 @@ func WithServerAddress(addr string) ClientOption { ...@@ -84,6 +85,13 @@ func WithServerAddress(addr string) ClientOption {
} }
} }
// @reconnectInterval is server address.
func WithReconnectInterval(reconnectInterval int) ClientOption {
return func(o *ClientOptions) {
o.reconnectInterval = reconnectInterval
}
}
// @num is connection number. // @num is connection number.
func WithConnectionNumber(num int) ClientOption { func WithConnectionNumber(num int) ClientOption {
return func(o *ClientOptions) { return func(o *ClientOptions) {
......
...@@ -776,7 +776,7 @@ func (s *session) stop() { ...@@ -776,7 +776,7 @@ func (s *session) stop() {
default: default:
s.once.Do(func() { s.once.Do(func() {
// let read/Write timeout asap // let read/Write timeout asap
now := wheel.Now() now := time.Now()
if conn := s.Conn(); conn != nil { if conn := s.Conn(); conn != nil {
conn.SetReadDeadline(now.Add(s.readTimeout())) conn.SetReadDeadline(now.Add(s.readTimeout()))
conn.SetWriteDeadline(now.Add(s.writeTimeout())) conn.SetWriteDeadline(now.Add(s.writeTimeout()))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment