Browse Source

bug fix to LeastPingStrategy

Shelikhoo 4 years ago
parent
commit
304b4feff6
2 changed files with 10 additions and 7 deletions
  1. 1 1
      app/router/router.go
  2. 9 6
      app/router/strategy_leastping.go

+ 1 - 1
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
 	}
 

+ 9 - 6
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()