Commit d63a5e25 authored by alexstocks's avatar alexstocks

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

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