Commit 600adac3 authored by u0x01's avatar u0x01

Merge from AlexStocks/getty

parent 533281db
......@@ -29,8 +29,8 @@ import (
)
const (
connInterval = 3e9 // 3s
connectTimeout = 5e9
connInterval = 5e8 // 500ms
connectTimeout = 3e9
maxTimes = 10
)
......@@ -175,7 +175,7 @@ func (c *client) dialUDP() Session {
}
// 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 {
conn.Close()
log.Warn("conn.Write(%s) = {length:%d, err:%s}", string(connectPingPackage), length, jerrors.ErrorStack(err))
......@@ -183,7 +183,7 @@ func (c *client) dialUDP() Session {
<-wheel.After(connInterval)
continue
}
conn.SetReadDeadline(wheel.Now().Add(1e9))
conn.SetReadDeadline(time.Now().Add(1e9))
length, err = conn.Read(buf)
if netErr, ok := jerrors.Cause(err).(net.Error); ok && netErr.Timeout() {
err = nil
......@@ -361,6 +361,10 @@ func (c *client) connect() {
// ss.RunEventLoop()
ss.(*session).run()
c.Lock()
if c.ssMap == nil {
c.Unlock()
break
}
c.ssMap[ss] = struct{}{}
c.Unlock()
ss.SetAttribute(sessionClientKey, c)
......@@ -408,8 +412,7 @@ func (c *client) reConnect() {
if maxTimes < times {
times = maxTimes
}
// time.Sleep(time.Duration(int64(times) * connInterval))
<-wheel.After(time.Duration(int64(times) * connInterval))
time.Sleep(time.Duration(int64(times) * int64(c.reconnectInterval)))
}
}
......
......@@ -240,7 +240,7 @@ func (t *gettyTCPConn) read(p []byte) (int, error) {
// Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded.
// 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 err = t.conn.SetReadDeadline(currentTime.Add(t.rTimeout)); err != nil {
return 0, jerrors.Trace(err)
......@@ -273,7 +273,7 @@ func (t *gettyTCPConn) Write(pkg interface{}) (int, error) {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// 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 err = t.conn.SetWriteDeadline(currentTime.Add(t.wTimeout)); err != nil {
return 0, jerrors.Trace(err)
......@@ -390,7 +390,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
// Optimization: update read deadline only if more than 25%
// of the last read deadline exceeded.
// 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 err = u.conn.SetReadDeadline(currentTime.Add(u.rTimeout)); err != nil {
return 0, nil, jerrors.Trace(err)
......@@ -438,7 +438,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// 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 err = u.conn.SetWriteDeadline(currentTime.Add(u.wTimeout)); err != nil {
return 0, jerrors.Trace(err)
......@@ -564,7 +564,7 @@ func (w *gettyWSConn) updateWriteDeadline() error {
// Optimization: update write deadline only if more than 25%
// of the last write deadline exceeded.
// 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 err = w.conn.SetWriteDeadline(currentTime.Add(w.wTimeout)); err != nil {
return jerrors.Trace(err)
......
module github.com/dubbogo/getty
require (
github.com/AlexStocks/getty v1.0.4
github.com/AlexStocks/goext v0.3.2
github.com/AlexStocks/log4go v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/AlexStocks/log4go v1.0.2
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/gorilla/websocket v1.4.0
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/kr/pretty v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7
go.etcd.io/etcd v3.3.13+incompatible // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
gopkg.in/yaml.v2 v2.2.2
)
This diff is collapsed.
......@@ -70,6 +70,7 @@ type ClientOption func(*ClientOptions)
type ClientOptions struct {
addr string
number int
reconnectInterval int// reConnect Interval
// 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.
......@@ -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.
func WithConnectionNumber(num int) ClientOption {
return func(o *ClientOptions) {
......
......@@ -776,7 +776,7 @@ func (s *session) stop() {
default:
s.once.Do(func() {
// let read/Write timeout asap
now := wheel.Now()
now := time.Now()
if conn := s.Conn(); conn != nil {
conn.SetReadDeadline(now.Add(s.readTimeout()))
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