client: reconnect more quickly if it's a dial error (#2240)

This commit is contained in:
fatedier 2021-02-18 16:15:35 +08:00 committed by GitHub
parent 976fd81d4d
commit 8fcd4f4a95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 20 additions and 14 deletions

View File

@ -17,6 +17,7 @@ package client
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net" "net"
@ -177,9 +178,16 @@ func (svr *Service) keepControllerWorking() {
if err != nil { if err != nil {
xl.Warn("reconnect to server error: %v", err) xl.Warn("reconnect to server error: %v", err)
time.Sleep(delayTime) time.Sleep(delayTime)
delayTime = delayTime * 2
if delayTime > maxDelayTime { opErr := &net.OpError{}
delayTime = maxDelayTime // 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 continue
} }

View File

@ -23,7 +23,6 @@ import (
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
// Visitor // Visitor
var ( var (
visitorConfTypeMap = map[string]reflect.Type{ visitorConfTypeMap = map[string]reflect.Type{
@ -64,7 +63,6 @@ type XTCPVisitorConf struct {
BaseVisitorConf `ini:",extends" json:"inline"` BaseVisitorConf `ini:",extends" json:"inline"`
} }
// DefaultVisitorConf creates a empty VisitorConf object by visitorType. // DefaultVisitorConf creates a empty VisitorConf object by visitorType.
// If visitorType doesn't exist, return nil. // If visitorType doesn't exist, return nil.
func DefaultVisitorConf(visitorType string) VisitorConf { func DefaultVisitorConf(visitorType string) VisitorConf {

View File

@ -19,8 +19,8 @@ import (
"github.com/fatedier/frp/pkg/consts" "github.com/fatedier/frp/pkg/consts"
"gopkg.in/ini.v1"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/ini.v1"
) )
const testVisitorPrefix = "test." const testVisitorPrefix = "test."

View File

@ -19,7 +19,7 @@ func TestCmdTCP(t *testing.T) {
if assert.NoError(err) { if assert.NoError(err) {
defer s.Stop() 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", 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"}) "-l", "10701", "-r", "20801", "-n", "tcp_test"})
@ -43,7 +43,7 @@ func TestCmdUDP(t *testing.T) {
if assert.NoError(err) { if assert.NoError(err) {
defer s.Stop() 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", 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"}) "-l", "10702", "-r", "20802", "-n", "udp_test"})
@ -67,7 +67,7 @@ func TestCmdHTTP(t *testing.T) {
if assert.NoError(err) { if assert.NoError(err) {
defer s.Stop() 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", 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"}) "-n", "udp_test", "-l", "10704", "--custom_domain", "127.0.0.1"})

View File

@ -175,7 +175,7 @@ func TestHealthCheck(t *testing.T) {
defer frpsProcess.Stop() defer frpsProcess.Stop()
} }
time.Sleep(100 * time.Millisecond) time.Sleep(500 * time.Millisecond)
frpcProcess := util.NewProcess(consts.FRPC_SUB_BIN_PATH, []string{"-c", frpcCfgPath}) frpcProcess := util.NewProcess(consts.FRPC_SUB_BIN_PATH, []string{"-c", frpcCfgPath})
err = frpcProcess.Start() err = frpcProcess.Start()

View File

@ -42,7 +42,7 @@ func TestMain(m *testing.M) {
panic(err) 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"}) p2 := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", "./auto_test_frpc.ini"})
if err = p2.Start(); err != nil { if err = p2.Start(); err != nil {
panic(err) panic(err)

View File

@ -56,7 +56,7 @@ func TestReconnect(t *testing.T) {
defer frpsProcess.Stop() defer frpsProcess.Stop()
} }
time.Sleep(200 * time.Millisecond) time.Sleep(500 * time.Millisecond)
frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath}) frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath})
err = frpcProcess.Start() err = frpcProcess.Start()

View File

@ -94,7 +94,7 @@ func TestReload(t *testing.T) {
defer frpsProcess.Stop() defer frpsProcess.Stop()
} }
time.Sleep(200 * time.Millisecond) time.Sleep(500 * time.Millisecond)
frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath}) frpcProcess := util.NewProcess(consts.FRPC_BIN_PATH, []string{"-c", frpcCfgPath})
err = frpcProcess.Start() err = frpcProcess.Start()

View File

@ -55,7 +55,7 @@ func TestConfTemplate(t *testing.T) {
defer frpsProcess.Stop() 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}) frpcProcess := util.NewProcess("env", []string{"FRP_TOKEN=123456", "TCP_REMOTE_PORT=20801", consts.FRPC_BIN_PATH, "-c", frpcCfgPath})
err = frpcProcess.Start() err = frpcProcess.Start()