From 304b4feff6d4d36d1539b14990b95596471ba3c6 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Thu, 8 Apr 2021 21:34:52 +0100 Subject: [PATCH] bug fix to LeastPingStrategy --- app/router/router.go | 2 +- app/router/strategy_leastping.go | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/router/router.go b/app/router/router.go index b67737a93..351f52333 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -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 } diff --git a/app/router/strategy_leastping.go b/app/router/strategy_leastping.go index e4244abfe..c1265c27d 100644 --- a/app/router/strategy_leastping.go +++ b/app/router/strategy_leastping.go @@ -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()