Commit d63a5e25 authored by alexstocks's avatar alexstocks

elasitic sleep time in client.go:(Client)RunTimeLoop

parent ee22cae5
...@@ -21,7 +21,9 @@ ...@@ -21,7 +21,9 @@
> >
> 5 session.go:(Session)SetConn > 5 session.go:(Session)SetConn
> >
> 6 version: 0.3.01 > 6 add elastic sleep time machanism in client.go:(Client)RunEventLoop
>
> 7 version: 0.3.01
- 2016/09/02 - 2016/09/02
> 1 add session.go:(gettyConn)close and session.go:(Session)dispose > 1 add session.go:(gettyConn)close and session.go:(Session)dispose
......
...@@ -21,6 +21,8 @@ import ( ...@@ -21,6 +21,8 @@ import (
const ( const (
defaultInterval = 3e9 // 3s defaultInterval = 3e9 // 3s
connectTimeout = 5e9
maxTimes = 10
) )
type empty struct{} type empty struct{}
...@@ -70,7 +72,7 @@ func (this *Client) dial() net.Conn { ...@@ -70,7 +72,7 @@ func (this *Client) dial() net.Conn {
if this.IsClosed() { if this.IsClosed() {
return nil return nil
} }
conn, err = net.DialTimeout("tcp", this.addr, this.interval) conn, err = net.DialTimeout("tcp", this.addr, connectTimeout)
if err == nil { if err == nil {
return conn return conn
} }
...@@ -129,7 +131,7 @@ func (this *Client) RunEventLoop(newSession SessionCallback) { ...@@ -129,7 +131,7 @@ func (this *Client) RunEventLoop(newSession SessionCallback) {
this.wg.Add(1) this.wg.Add(1)
go func() { go func() {
var num, max int var num, max, times int
defer this.wg.Done() defer this.wg.Done()
this.Lock() this.Lock()
...@@ -145,10 +147,16 @@ func (this *Client) RunEventLoop(newSession SessionCallback) { ...@@ -145,10 +147,16 @@ func (this *Client) RunEventLoop(newSession SessionCallback) {
num = this.sessionNum() num = this.sessionNum()
// log.Info("current client connction number:%d", num) // log.Info("current client connction number:%d", num)
if max <= num { if max <= num {
time.Sleep(this.interval) times++
if maxTimes < times {
times = maxTimes
}
time.Sleep(time.Duration(times * defaultInterval))
continue continue
} }
times = 0
this.connect() this.connect()
// time.Sleep(this.interval) // build this.number connections asap
} }
}() }()
} }
......
...@@ -14,21 +14,12 @@ import ( ...@@ -14,21 +14,12 @@ import (
"encoding/binary" "encoding/binary"
"net" "net"
"strconv" "strconv"
"time"
) )
func HostAddress(host string, port int) string { func HostAddress(host string, port int) string {
return net.JoinHostPort(host, strconv.Itoa(port)) return net.JoinHostPort(host, strconv.Itoa(port))
} }
func dial(addr string) (net.Conn, error) {
return net.Dial("tcp", addr)
}
func dialTimeout(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", addr, timeout)
}
//////////////////////////////////////// ////////////////////////////////////////
// enc/dec // enc/dec
//////////////////////////////////////// ////////////////////////////////////////
......
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