From 553dc29cf8a3577077c148952a67c071de148638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Wed, 17 Apr 2024 14:14:35 +0800 Subject: [PATCH 1/7] fix:1.stop to reconnect when face EOF error 2.exit reconnect when reconnection times exceeds the connection numbers --- client.go | 27 ++++++++++++++++----------- session.go | 12 +++++++----- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index 3c75a17..ad9ce75 100644 --- a/client.go +++ b/client.go @@ -28,14 +28,14 @@ import ( "sync" "sync/atomic" "time" -) -import ( - "github.com/dubbogo/gost/bytes" - "github.com/dubbogo/gost/net" + gxbytes "github.com/dubbogo/gost/bytes" + + gxnet "github.com/dubbogo/gost/net" + gxsync "github.com/dubbogo/gost/sync" - gxtime "github.com/dubbogo/gost/time" + gxtime "github.com/dubbogo/gost/time" "github.com/gorilla/websocket" perrors "github.com/pkg/errors" @@ -52,7 +52,8 @@ var ( sessionClientKey = "session-client-owner" connectPingPackage = []byte("connect-ping") - clientID = EndPointID(0) + clientID = EndPointID(0) + ignoreReconnectKey = "ignore-reconnect" ) type Client interface { @@ -397,6 +398,7 @@ func (c *client) connect() { c.ssMap[ss] = struct{}{} c.Unlock() ss.SetAttribute(sessionClientKey, c) + ss.SetAttribute(ignoreReconnectKey, false) break } // don't distinguish between tcp connection and websocket connection. Because @@ -422,7 +424,7 @@ 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 - + var maxDuraion int64 max = c.number interval = c.reconnectInterval if interval == 0 { @@ -435,15 +437,17 @@ func (c *client) reConnect() { } num = c.sessionNum() - if max <= num { + if max <= num || max < times { //Exit when the number of connection pools is sufficient or the reconnection times exceeds the connections numbers. break } c.connect() times++ - if maxTimes < times { - times = maxTimes + if times > maxTimes { + maxDuraion = int64(maxTimes) * int64(interval) + } else { + maxDuraion = int64(times) * int64(interval) } - <-gxtime.After(time.Duration(int64(times) * int64(interval))) + <-gxtime.After(time.Duration(maxDuraion)) } } @@ -457,6 +461,7 @@ func (c *client) stop() { c.Lock() for s := range c.ssMap { s.RemoveAttribute(sessionClientKey) + s.RemoveAttribute(ignoreReconnectKey) s.Close() } c.ssMap = nil diff --git a/session.go b/session.go index 9e88b6f..8a74cc2 100644 --- a/session.go +++ b/session.go @@ -27,13 +27,12 @@ import ( "runtime" "sync" "time" -) -import ( gxbytes "github.com/dubbogo/gost/bytes" + gxcontext "github.com/dubbogo/gost/context" - gxtime "github.com/dubbogo/gost/time" + gxtime "github.com/dubbogo/gost/time" "github.com/gorilla/websocket" perrors "github.com/pkg/errors" @@ -671,6 +670,8 @@ func (s *session) handleTCPPackage() error { } if perrors.Cause(err) == io.EOF { log.Infof("%s, session.conn read EOF, client send over, session exit", s.sessionToken()) + //when read EOF, means that the peer has closed the connection, stop to reconnect to maintain the connection pool. + s.SetAttribute(ignoreReconnectKey, true) err = nil exit = true if bufLen != 0 { @@ -858,8 +859,9 @@ func (s *session) stop() { conn.SetWriteDeadline(now.Add(s.WriteTimeout())) } close(s.done) - c := s.GetAttribute(sessionClientKey) - if clt, ok := c.(*client); ok { + clt, cltFound := s.GetAttribute(sessionClientKey).(*client) + ignoreReconnect, flagFound := s.GetAttribute(ignoreReconnectKey).(bool) + if cltFound && flagFound && !ignoreReconnect { clt.reConnect() } }) From 52e6c4aef28a8f3a5d9e347d9156c472d1425ca4 Mon Sep 17 00:00:00 2001 From: No-SilverBullet <112961281+No-SilverBullet@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:55:18 +0800 Subject: [PATCH 2/7] restore import farmat --- session.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/session.go b/session.go index 8a74cc2..587f654 100644 --- a/session.go +++ b/session.go @@ -27,12 +27,13 @@ import ( "runtime" "sync" "time" +) +import ( gxbytes "github.com/dubbogo/gost/bytes" - gxcontext "github.com/dubbogo/gost/context" - gxtime "github.com/dubbogo/gost/time" + "github.com/gorilla/websocket" perrors "github.com/pkg/errors" From 4cbf2c136c8a0f9e37456804ae142c2b4d113377 Mon Sep 17 00:00:00 2001 From: No-SilverBullet <112961281+No-SilverBullet@users.noreply.github.com> Date: Wed, 17 Apr 2024 17:56:36 +0800 Subject: [PATCH 3/7] restore import format --- client.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index ad9ce75..b9cdcf1 100644 --- a/client.go +++ b/client.go @@ -28,14 +28,14 @@ import ( "sync" "sync/atomic" "time" +) - gxbytes "github.com/dubbogo/gost/bytes" - - gxnet "github.com/dubbogo/gost/net" - +import ( + "github.com/dubbogo/gost/bytes" + "github.com/dubbogo/gost/net" gxsync "github.com/dubbogo/gost/sync" - gxtime "github.com/dubbogo/gost/time" + "github.com/gorilla/websocket" perrors "github.com/pkg/errors" From d3f5be39904c45825894b5e1fc6d40db1aeb4b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?xinfan=2Ewu=28=E5=90=B4=E6=AD=86=E5=B8=86=29?= Date: Wed, 17 Apr 2024 18:36:30 +0800 Subject: [PATCH 4/7] =?UTF-8?q?style:=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index b9cdcf1..84120c2 100644 --- a/client.go +++ b/client.go @@ -56,6 +56,10 @@ var ( ignoreReconnectKey = "ignore-reconnect" ) +var ( + num, max, times, interval int + maxDuraion int64 +) type Client interface { EndPoint } @@ -423,8 +427,6 @@ 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 - var maxDuraion int64 max = c.number interval = c.reconnectInterval if interval == 0 { @@ -437,7 +439,8 @@ func (c *client) reConnect() { } 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. + if max <= num || max < times { + //Exit when the number of connection pools is sufficient or the reconnection times exceeds the connections numbers. break } c.connect() From 0c0272c19193926c636b24ceb3e8cf47f3a1ced7 Mon Sep 17 00:00:00 2001 From: No-SilverBullet <112961281+No-SilverBullet@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:14:43 +0800 Subject: [PATCH 5/7] style:format code --- client.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 84120c2..3aa9be2 100644 --- a/client.go +++ b/client.go @@ -58,8 +58,9 @@ var ( var ( num, max, times, interval int - maxDuraion int64 + maxDuraion int64 ) + type Client interface { EndPoint } From 05a6a9a66222c9fadb95325047fb2c194bc9d20b Mon Sep 17 00:00:00 2001 From: No-SilverBullet <112961281+No-SilverBullet@users.noreply.github.com> Date: Thu, 18 Apr 2024 16:31:36 +0800 Subject: [PATCH 6/7] style:change the declaration location ofreconnect-related variables --- client.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 3aa9be2..045c6e1 100644 --- a/client.go +++ b/client.go @@ -56,11 +56,6 @@ var ( ignoreReconnectKey = "ignore-reconnect" ) -var ( - num, max, times, interval int - maxDuraion int64 -) - type Client interface { EndPoint } @@ -428,6 +423,10 @@ 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 + maxDuraion int64 + ) max = c.number interval = c.reconnectInterval if interval == 0 { @@ -440,7 +439,7 @@ func (c *client) reConnect() { } num = c.sessionNum() - if max <= num || max < times { + if max <= num || max < times { //Exit when the number of connection pools is sufficient or the reconnection times exceeds the connections numbers. break } From b33c1e635a86f92202023807647067dac72f49c4 Mon Sep 17 00:00:00 2001 From: No-SilverBullet <112961281+No-SilverBullet@users.noreply.github.com> Date: Thu, 18 Apr 2024 17:26:14 +0800 Subject: [PATCH 7/7] fix:maxDuration typo --- client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 045c6e1..7f8b557 100644 --- a/client.go +++ b/client.go @@ -425,7 +425,7 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) { func (c *client) reConnect() { var ( num, max, times, interval int - maxDuraion int64 + maxDuration int64 ) max = c.number interval = c.reconnectInterval @@ -446,11 +446,11 @@ func (c *client) reConnect() { c.connect() times++ if times > maxTimes { - maxDuraion = int64(maxTimes) * int64(interval) + maxDuration = int64(maxTimes) * int64(interval) } else { - maxDuraion = int64(times) * int64(interval) + maxDuration = int64(times) * int64(interval) } - <-gxtime.After(time.Duration(maxDuraion)) + <-gxtime.After(time.Duration(maxDuration)) } }