Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/go 1.21 #4

Merged
merged 3 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: |
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion coredns/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/networkservicemesh/fanout/coredns

go 1.22
go 1.21

require (
github.com/coredns/coredns v1.11.3
Expand Down
4 changes: 2 additions & 2 deletions fanout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/networkservicemesh/fanout

go 1.22
go 1.21

require (
github.com/coredns/caddy v1.1.1
Expand Down
11 changes: 7 additions & 4 deletions internal/selector/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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]
Expand Down
18 changes: 9 additions & 9 deletions internal/selector/rand_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package selector

import (
"math/rand/v2"
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -20,42 +20,42 @@ 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 {
t.Run(name, func(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())
}

Expand Down
2 changes: 1 addition & 1 deletion internal/selector/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
2 changes: 1 addition & 1 deletion setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Loading