Skip to content

Commit

Permalink
optimize: force to choose one if there is only one node in the group (#…
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
mzz2017 and dae-prow[bot] authored Aug 3, 2023
1 parent 3a6bf3e commit e5d3f5b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions component/outbound/dialer_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e5d3f5b

Please sign in to comment.