bug fix to LeastPingStrategy

This commit is contained in:
Shelikhoo 2021-04-08 21:34:52 +01:00
parent 017a11cfd5
commit 304b4feff6
No known key found for this signature in database
GPG Key ID: C4D5E79D22B25316
2 changed files with 10 additions and 7 deletions

View File

@ -6,7 +6,6 @@ package router
import (
"context"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/features/dns"
@ -41,6 +40,7 @@ func (r *Router) Init(ctx context.Context, config *Config, d dns.Client, ohm out
if err != nil {
return err
}
balancer.InjectContext(ctx)
r.balancers[rule.Tag] = balancer
}

View File

@ -2,10 +2,10 @@ package router
import (
"context"
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/observatory"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/app/observatory"
"github.com/v2fly/v2ray-core/v4/features/extension"
)
@ -15,14 +15,17 @@ type LeastPingStrategy struct {
}
func (l *LeastPingStrategy) InjectContext(ctx context.Context) {
common.Must(core.RequireFeatures(ctx, func(observatory extension.Observatory) error {
l.observatory = observatory
return nil
}))
l.ctx = ctx
}
func (l *LeastPingStrategy) PickOutbound(strings []string) string {
if l.observatory == nil {
common.Must(core.RequireFeatures(l.ctx, func(observatory extension.Observatory) error {
l.observatory = observatory
return nil
}))
}
observeReport, err := l.observatory.GetObservation(l.ctx)
if err != nil {
newError("cannot get observe report").Base(err).WriteToLog()