Skip to content

Commit

Permalink
Merge pull request #124 from No-SilverBullet/master
Browse files Browse the repository at this point in the history
rename the variables in TCP reconnect function and some default constants.
  • Loading branch information
AlexStocks authored Jun 15, 2024
2 parents a96fbf7 + 84504ea commit ac07194
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ import (
)

const (
reconnectInterval = 3e8 // 300ms
connectInterval = 5e8 // 500ms
connectTimeout = 3e9
maxTimes = 10
defaultReconnectInterval = 3e8 // 300ms
connectInterval = 5e8 // 500ms
connectTimeout = 3e9
defaultMaxReconnectAttempts = 10
)

var (
Expand Down Expand Up @@ -424,33 +424,33 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) {
// a for-loop connect to make sure the connection pool is valid
func (c *client) reConnect() {
var (
num, max, times, interval int
maxDuration int64
sessionNum, maxReconnectAttempts, reconnectAttempts, reconnectInterval int
maxReconnectInterval int64
)
max = c.number
interval = c.reconnectInterval
if interval == 0 {
interval = reconnectInterval
maxReconnectAttempts = c.number
reconnectInterval = c.reconnectInterval
if reconnectInterval == 0 {
reconnectInterval = defaultReconnectInterval
}
for {
if c.IsClosed() {
log.Warnf("client{peer:%s} goroutine exit now.", c.addr)
break
}

num = c.sessionNum()
if max <= num || max < times {
//Exit when the number of connection pools is sufficient or the reconnection times exceeds the connections numbers.
sessionNum = c.sessionNum()
if maxReconnectAttempts <= sessionNum || maxReconnectAttempts < reconnectAttempts {
//exit reconnect when the number of connection pools is sufficient or the current reconnection attempts exceeds the max reconnection attempts.
break
}
c.connect()
times++
if times > maxTimes {
maxDuration = int64(maxTimes) * int64(interval)
reconnectAttempts++
if reconnectAttempts > defaultMaxReconnectAttempts {
maxReconnectInterval = int64(defaultMaxReconnectAttempts) * int64(reconnectInterval)
} else {
maxDuration = int64(times) * int64(interval)
maxReconnectInterval = int64(reconnectAttempts) * int64(reconnectInterval)
}
<-gxtime.After(time.Duration(maxDuration))
<-gxtime.After(time.Duration(maxReconnectInterval))
}
}

Expand Down

0 comments on commit ac07194

Please sign in to comment.