From 28356c2233d981b9dc620c61ccc78b1bbcf576e6 Mon Sep 17 00:00:00 2001 From: glebkin Date: Wed, 24 Jul 2024 17:51:57 +0300 Subject: [PATCH 1/3] Set Go version to 1.21 Signed-off-by: glebkin --- coredns/go.mod | 2 +- fanout.go | 4 ++-- go.mod | 2 +- internal/selector/rand.go | 11 +++++++---- internal/selector/rand_test.go | 18 +++++++++--------- internal/selector/simple_test.go | 2 +- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/coredns/go.mod b/coredns/go.mod index dd0e950..d36bb04 100644 --- a/coredns/go.mod +++ b/coredns/go.mod @@ -1,6 +1,6 @@ module github.com/networkservicemesh/fanout/coredns -go 1.22 +go 1.21 require ( github.com/coredns/coredns v1.11.3 diff --git a/fanout.go b/fanout.go index 8b27ca8..8be519f 100644 --- a/fanout.go +++ b/fanout.go @@ -124,7 +124,7 @@ func (f *Fanout) runWorkers(ctx context.Context, req *request.Request) chan *res responseCh := make(chan *response, f.serverCount) go func() { defer close(workerChannel) - for range f.serverCount { + for i := 0; i < f.serverCount; i++ { select { case <-ctx.Done(): return @@ -137,7 +137,7 @@ func (f *Fanout) runWorkers(ctx context.Context, req *request.Request) chan *res var wg sync.WaitGroup wg.Add(f.workerCount) - for range f.workerCount { + for i := 0; i < f.workerCount; i++ { go func() { defer wg.Done() for c := range workerChannel { diff --git a/go.mod b/go.mod index 2428a55..2f3703b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/networkservicemesh/fanout -go 1.22 +go 1.21 require ( github.com/coredns/caddy v1.1.1 diff --git a/internal/selector/rand.go b/internal/selector/rand.go index 13021b7..3e69945 100644 --- a/internal/selector/rand.go +++ b/internal/selector/rand.go @@ -17,7 +17,10 @@ // Package selector implements weighted random selection algorithm package selector -import "math/rand/v2" +import ( + "math/rand" + "time" +) // WeightedRand selector picks elements randomly based on their weights type WeightedRand[T any] struct { @@ -34,7 +37,7 @@ func NewWeightedRandSelector[T any](values []T, weights []int) *WeightedRand[T] weights: make([]int, len(weights)), totalWeight: 0, //nolint:gosec // it's overhead to use crypto/rand here - r: rand.New(rand.NewPCG(rand.Uint64(), rand.Uint64())), + r: rand.New(rand.NewSource(time.Now().UnixNano())), } // copy the underlying array values as we're going to modify content of slices copy(wrs.values, values) @@ -54,10 +57,10 @@ func (wrs *WeightedRand[T]) Pick() T { return defaultVal } - rNum := wrs.r.IntN(wrs.totalWeight) + 1 + rNum := wrs.r.Intn(wrs.totalWeight) + 1 sum := 0 - for i := range len(wrs.values) { + for i := 0; i < len(wrs.values); i++ { sum += wrs.weights[i] if sum >= rNum { wrs.totalWeight -= wrs.weights[i] diff --git a/internal/selector/rand_test.go b/internal/selector/rand_test.go index 8dd1b8d..df972b9 100644 --- a/internal/selector/rand_test.go +++ b/internal/selector/rand_test.go @@ -1,7 +1,7 @@ package selector import ( - "math/rand/v2" + "math/rand" "testing" "github.com/stretchr/testify/assert" @@ -20,31 +20,31 @@ func TestWeightedRand_Pick(t *testing.T) { values: []string{"a", "b", "c", "d", "e", "f", "g"}, weights: []int{100, 100, 100, 100, 100, 100, 100}, picksCount: 7, - expected: []string{"f", "d", "g", "e", "a", "c", "b"}, + expected: []string{"b", "a", "d", "f", "g", "c", "e"}, }, "pick_all_different_weight": { values: []string{"a", "b", "c", "d", "e", "f", "g"}, weights: []int{100, 70, 10, 50, 100, 30, 50}, picksCount: 7, - expected: []string{"e", "d", "f", "g", "a", "c", "b"}, + expected: []string{"d", "a", "e", "b", "g", "c", "f"}, }, "pick_some_same_weight": { values: []string{"a", "b", "c", "d", "e", "f", "g"}, weights: []int{100, 100, 100, 100, 100, 100, 100}, picksCount: 3, - expected: []string{"f", "d", "g"}, + expected: []string{"b", "a", "d"}, }, "pick_some_different_weight": { values: []string{"a", "b", "c", "d", "e", "f", "g"}, weights: []int{100, 70, 10, 50, 100, 30, 50}, picksCount: 3, - expected: []string{"e", "d", "f"}, + expected: []string{"d", "a", "e"}, }, "pick_more_than_available": { values: []string{"a", "b", "c"}, - weights: []int{100, 70, 10}, + weights: []int{70, 10, 100}, picksCount: 4, - expected: []string{"b", "a", "c", ""}, + expected: []string{"a", "c", "b", ""}, }, } for name, tc := range testCases { @@ -52,10 +52,10 @@ func TestWeightedRand_Pick(t *testing.T) { wrs := NewWeightedRandSelector(tc.values, tc.weights) // init rand with constant seed to get predefined result //nolint:gosec - wrs.r = rand.New(rand.NewPCG(1, 2)) + wrs.r = rand.New(rand.NewSource(1)) actual := make([]string, 0, tc.picksCount) - for range tc.picksCount { + for i := 0; i < tc.picksCount; i++ { actual = append(actual, wrs.Pick()) } diff --git a/internal/selector/simple_test.go b/internal/selector/simple_test.go index a6772dd..6e43d70 100644 --- a/internal/selector/simple_test.go +++ b/internal/selector/simple_test.go @@ -35,7 +35,7 @@ func TestSimple_Pick(t *testing.T) { wrs := NewSimpleSelector(tc.values) actual := make([]string, 0, tc.picksCount) - for range tc.picksCount { + for i := 0; i < tc.picksCount; i++ { actual = append(actual, wrs.Pick()) } From d5127c65a2deb47d64e14851b1f35571fdf31406 Mon Sep 17 00:00:00 2001 From: glebkin Date: Wed, 24 Jul 2024 17:54:32 +0300 Subject: [PATCH 2/3] Set Go version to 1.21 Signed-off-by: glebkin --- .github/workflows/ci.yaml | 10 +++++----- .github/workflows/push.yaml | 2 +- .github/workflows/release.yaml | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b56c912..a121d46 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -28,7 +28,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-go@v1 with: - go-version: 1.22 + go-version: 1.21 - run: | go build -race ./... test: @@ -38,7 +38,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-go@v1 with: - go-version: 1.22 + go-version: 1.21 - name: Install gotestsum run: go install gotest.tools/gotestsum@v0.4.0 - name: Run tests @@ -68,7 +68,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-go@v1 with: - go-version: 1.22 + go-version: 1.21 - name: build coredns binary run: | go build -o coredns/coredns coredns/main.go @@ -111,7 +111,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-go@v1 with: - go-version: 1.22 + go-version: 1.21 - run: go mod tidy - name: Check for changes in go.mod or go.sum run: | @@ -124,7 +124,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-go@v1 with: - go-version: 1.22 + go-version: 1.21 - name: Install go-header run: 'go install github.com/denis-tingajkin/go-header@v0.2.2' - name: Run go-header diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml index c7a35f5..f067f7d 100644 --- a/.github/workflows/push.yaml +++ b/.github/workflows/push.yaml @@ -18,7 +18,7 @@ jobs: - uses: actions/setup-go@v1 with: - go-version: 1.22 + go-version: 1.21 - name: "Build coredns binary" run: | go build -o coredns/coredns coredns/main.go diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c126c2d..f90449d 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -46,7 +46,7 @@ jobs: - uses: actions/setup-go@v1 with: - go-version: 1.22 + go-version: 1.21 - name: "Build coredns binary" run: | go build -o coredns/coredns coredns/main.go From 5058cb0108ab607ab601c29d26b777ca5a3938aa Mon Sep 17 00:00:00 2001 From: glebkin Date: Wed, 24 Jul 2024 17:56:16 +0300 Subject: [PATCH 3/3] Set Go version to 1.21 Signed-off-by: glebkin --- setup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.go b/setup.go index f21c81c..b5b746a 100644 --- a/setup.go +++ b/setup.go @@ -130,7 +130,7 @@ func parsefanoutStanza(c *caddyfile.Dispenser) (*Fanout, error) { f.serverCount = len(toHosts) } if len(f.loadFactor) == 0 { - for range len(toHosts) { + for i := 0; i < len(toHosts); i++ { f.loadFactor = append(f.loadFactor, maxLoadFactor) } }