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>
This commit is contained in:
mzz 2023-08-03 21:16:58 +08:00 committed by GitHub
parent 3a6bf3e848
commit e5d3f5ba58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 {