mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-11 00:09:18 +07:00
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:
@ -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.
|
// 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) {
|
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) {
|
if !strictIpVersion && errors.Is(err, NoAliveDialerError) {
|
||||||
networkType.IpVersion = (consts.IpVersion_X - networkType.IpVersion.ToIpVersionType()).ToIpVersionStr()
|
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 {
|
if len(g.Dialers) == 0 {
|
||||||
return nil, 0, fmt.Errorf("no dialer in this group")
|
return nil, 0, fmt.Errorf("no dialer in this group")
|
||||||
}
|
}
|
||||||
|
|
||||||
a := g.MustGetAliveDialerSet(networkType)
|
a := g.MustGetAliveDialerSet(networkType)
|
||||||
|
switch policy.Policy {
|
||||||
switch g.selectionPolicy.Policy {
|
|
||||||
case consts.DialerSelectionPolicy_Random:
|
case consts.DialerSelectionPolicy_Random:
|
||||||
d := a.GetRand()
|
d := a.GetRand()
|
||||||
if d == nil {
|
if d == nil {
|
||||||
|
Reference in New Issue
Block a user