From 8fcd4f4a954e204a94472b727d46d34f7543f712 Mon Sep 17 00:00:00 2001 From: fatedier Date: Thu, 18 Feb 2021 16:15:35 +0800 Subject: [PATCH] client: reconnect more quickly if it's a dial error (#2240) --- client/service.go | 14 +++++++++++--- pkg/config/visitor.go | 2 -- pkg/config/visitor_test.go | 2 +- tests/ci/cmd_test.go | 6 +++--- tests/ci/health/health_test.go | 2 +- tests/ci/normal_test.go | 2 +- tests/ci/reconnect_test.go | 2 +- tests/ci/reload_test.go | 2 +- tests/ci/template_test.go | 2 +- 9 files changed, 20 insertions(+), 14 deletions(-) diff --git a/client/service.go b/client/service.go index 2b1b38aa..56b70db8 100644 --- a/client/service.go +++ b/client/service.go @@ -17,6 +17,7 @@ package client import ( "context" "crypto/tls" + "errors" "fmt" "io/ioutil" "net" @@ -177,9 +178,16 @@ func (svr *Service) keepControllerWorking() { if err != nil { xl.Warn("reconnect to server error: %v", err) time.Sleep(delayTime) - delayTime = delayTime * 2 - if delayTime > maxDelayTime { - delayTime = maxDelayTime + + opErr := &net.OpError{} + // quick retry for dial error + if errors.As(err, &opErr) && opErr.Op == "dial" { + delayTime = 2 * time.Second + } else { + delayTime = delayTime * 2 + if delayTime > maxDelayTime { + delayTime = maxDelayTime + } } continue } diff --git a/pkg/config/visitor.go b/pkg/config/visitor.go index f3552b32..11959eca 100644 --- a/pkg/config/visitor.go +++ b/pkg/config/visitor.go @@ -23,7 +23,6 @@ import ( "gopkg.in/ini.v1" ) - // Visitor var ( visitorConfTypeMap = map[string]reflect.Type{ @@ -64,7 +63,6 @@ type XTCPVisitorConf struct { BaseVisitorConf `ini:",extends" json:"inline"` } - // DefaultVisitorConf creates a empty VisitorConf object by visitorType. // If visitorType doesn't exist, return nil. func DefaultVisitorConf(visitorType string) VisitorConf { diff --git a/pkg/config/visitor_test.go b/pkg/config/visitor_test.go index 98b2f097..ce200ed0 100644 --- a/pkg/config/visitor_test.go +++ b/pkg/config/visitor_test.go @@ -19,8 +19,8 @@ import ( "github.com/fatedier/frp/pkg/consts" - "gopkg.in/ini.v1" "github.com/stretchr/testify/assert" + "gopkg.in/ini.v1" ) const testVisitorPrefix = "test." diff --git a/tests/ci/cmd_test.go b/tests/ci/cmd_test.go index 8a0b4775..4a98c446 100644 --- a/tests/ci/cmd_test.go +++ b/tests/ci/cmd_test.go @@ -19,7 +19,7 @@ func TestCmdTCP(t *testing.T) { if assert.NoError(err) { defer s.Stop() } - time.Sleep(200 * time.Millisecond) + time.Sleep(500 * time.Millisecond) c := util.NewProcess(consts.FRPC_BIN_PATH, []string{"tcp", "-s", "127.0.0.1:20000", "-t", "123", "-u", "test", "-l", "10701", "-r", "20801", "-n", "tcp_test"}) @@ -43,7 +43,7 @@ func TestCmdUDP(t *testing.T) { if assert.NoError(err) { defer s.Stop() } - time.Sleep(200 * time.Millisecond) + time.Sleep(500 * time.Millisecond) c := util.NewProcess(consts.FRPC_BIN_PATH, []string{"udp", "-s", "127.0.0.1:20000", "-t", "123", "-u", "test", "-l", "10702", "-r", "20802", "-n", "udp_test"}) @@ -67,7 +67,7 @@ func TestCmdHTTP(t *testing.T) { if assert.NoError(err) { defer s.Stop() } - time.Sleep(200 * time.Millisecond) + time.Sleep(500 * time.Millisecond) c := util.NewProcess(consts.FRPC_BIN_PATH, []string{"http", "-s", "127.0.0.1:20000", "-t", "123", "-u", "test", "-n", "udp_test", "-l", "10704", "--custom_domain", "127.0.0.1"}) diff --git a/tests/ci/health/health_test.go b/tests/ci/health/health_test.go index 0e75ad22..102511a6 100644 --- a/tests/ci/health/health_test.go +++ b/tests/ci/health/health_test.go @@ -175,7 +175,7 @@ func TestHealthCheck(t *testing.T) { defer frpsProcess.Stop() } - time.Sleep(100 * time.Millisecond) + time.Sleep(500 * time.Millisecond) frpcProcess := util.NewProcess(consts.FRPC_SUB_BIN_PATH, []string{"-c", frpcCfgPath}) err = frpcProcess.Start() diff --git a/tests/ci/normal_test.go b/tests/ci/normal_test.go index e9e34945..f1dba7a1 100644 --- a/tests/ci/normal_test.go +++ b/tests/ci/normal_test.go @@ -42,7 +42,7 @@ func TestMain(m *testing.M) { panic(err) } - time.Sleep(200 * time.Millisecond) + time.Sleep(500 * time.Millisecond) p2 := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc.ini"}) if err = p2.Start(); err != nil { panic(err) diff --git a/tests/ci/reconnect_test.go b/tests/ci/reconnect_test.go index 86ab317b..6455347f 100644 --- a/tests/ci/reconnect_test.go +++ b/tests/ci/reconnect_test.go @@ -56,7 +56,7 @@ func TestReconnect(t *testing.T) { defer frpsProcess.Stop() } - time.Sleep(200 * time.Millisecond) + time.Sleep(500 * time.Millisecond) frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath}) err = frpcProcess.Start() diff --git a/tests/ci/reload_test.go b/tests/ci/reload_test.go index 73a7a091..ac160fa5 100644 --- a/tests/ci/reload_test.go +++ b/tests/ci/reload_test.go @@ -94,7 +94,7 @@ func TestReload(t *testing.T) { defer frpsProcess.Stop() } - time.Sleep(200 * time.Millisecond) + time.Sleep(500 * time.Millisecond) frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath}) err = frpcProcess.Start() diff --git a/tests/ci/template_test.go b/tests/ci/template_test.go index f1361212..0c843cd8 100644 --- a/tests/ci/template_test.go +++ b/tests/ci/template_test.go @@ -55,7 +55,7 @@ func TestConfTemplate(t *testing.T) { defer frpsProcess.Stop() } - time.Sleep(200 * time.Millisecond) + time.Sleep(500 * time.Millisecond) frpcProcess := util.NewProcess("env", []string{"FRP_TOKEN=123456", "TCP_REMOTE_PORT=20801", consts.FRPC_BIN_PATH, "-c", frpcCfgPath}) err = frpcProcess.Start()