From e5d3f5ba581afa2eb97ded4a096ecd3e6942325b Mon Sep 17 00:00:00 2001 From: mzz <2017@duck.com> Date: Thu, 3 Aug 2023 21:16:58 +0800 Subject: [PATCH] optimize: force to choose one if there is only one node in the group (#251) * optimize: force to choose if there is only one node in the group * chore: remove log --------- Co-authored-by: dae-bot[bot] <136105375+dae-bot[bot]@users.noreply.github.com> --- component/outbound/dialer_group.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/component/outbound/dialer_group.go b/component/outbound/dialer_group.go index 5adcce090..0fefff376 100644 --- a/component/outbound/dialer_group.go +++ b/component/outbound/dialer_group.go @@ -217,22 +217,31 @@ func (d *DialerGroup) MustGetAliveDialerSet(typ *dialer.NetworkType) *dialer.Ali // Select selects a dialer from group according to selectionPolicy. If 'strictIpVersion' is false and no alive dialer, it will fallback to another ipversion. func (g *DialerGroup) Select(networkType *dialer.NetworkType, strictIpVersion bool) (d *dialer.Dialer, latency time.Duration, err error) { - d, latency, err = g._select(networkType) + policy := g.selectionPolicy + d, latency, err = g._select(networkType, policy) if !strictIpVersion && errors.Is(err, NoAliveDialerError) { networkType.IpVersion = (consts.IpVersion_X - networkType.IpVersion.ToIpVersionType()).ToIpVersionStr() - return g._select(networkType) + return g._select(networkType, policy) } - return d, latency, err + if err == nil { + return d, latency, nil + } + if errors.Is(err, NoAliveDialerError) && len(g.Dialers) == 1 { + // There is only one dialer in this group. Just choose it instead of return error. + return g._select(networkType, &DialerSelectionPolicy{ + Policy: consts.DialerSelectionPolicy_Fixed, + FixedIndex: 0, + }) + } + return nil, latency, err } -func (g *DialerGroup) _select(networkType *dialer.NetworkType) (d *dialer.Dialer, latency time.Duration, err error) { +func (g *DialerGroup) _select(networkType *dialer.NetworkType, policy *DialerSelectionPolicy) (d *dialer.Dialer, latency time.Duration, err error) { if len(g.Dialers) == 0 { return nil, 0, fmt.Errorf("no dialer in this group") } - a := g.MustGetAliveDialerSet(networkType) - - switch g.selectionPolicy.Policy { + switch policy.Policy { case consts.DialerSelectionPolicy_Random: d := a.GetRand() if d == nil {