From f5d190ab1542b96688353eeb3d3d5c46fbad8b7c Mon Sep 17 00:00:00 2001
From: Irine Sistiana <49315432+IrineSistiana@users.noreply.github.com>
Date: Wed, 11 Dec 2024 20:49:42 +0800
Subject: [PATCH 1/2] forward: avoid picking same upstream if concurrent > 2

---
 plugin/executable/forward/forward.go | 4 +++-
 plugin/executable/forward/utils.go   | 5 -----
 2 files changed, 3 insertions(+), 6 deletions(-)

--- a/plugin/executable/forward/forward.go
+++ b/plugin/executable/forward/forward.go
@@ -24,6 +24,7 @@ import (
 	"crypto/tls"
 	"errors"
 	"fmt"
+	"math/rand/v2"
 	"strings"
 	"time"
 
@@ -261,8 +262,9 @@ func (f *Forward) exchange(ctx context.C
 	done := make(chan struct{})
 	defer close(done)
 
+	r := rand.IntN(len(us))
 	for i := 0; i < concurrent; i++ {
-		u := randPick(us)
+		u := us[(r+i)%len(us)]
 		qc := copyPayload(queryPayload)
 		go func(uqid uint32, question dns.Question) {
 			defer pool.ReleaseBuf(qc)
--- a/plugin/executable/forward/utils.go
+++ b/plugin/executable/forward/utils.go
@@ -21,7 +21,6 @@ package fastforward
 
 import (
 	"context"
-	"math/rand"
 	"time"
 
 	"github.com/IrineSistiana/mosdns/v5/pkg/pool"
@@ -153,10 +152,6 @@ func (q *queryInfo) MarshalLogObject(enc
 	return nil
 }
 
-func randPick[T any](s []T) T {
-	return s[rand.Intn(len(s))]
-}
-
 func copyPayload(b *[]byte) *[]byte {
 	bc := pool.GetBuf(len(*b))
 	copy(*bc, *b)
