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

init rand once #65

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 5 additions & 6 deletions fanout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package fanout
import (
"context"
"fmt"
"math/rand"
"net"
"os"
"strings"
Expand All @@ -29,15 +30,13 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"go.uber.org/goleak"

"github.com/coredns/caddy"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/stretchr/testify/suite"

"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.uber.org/goleak"
)

const testQuery = "example1."
Expand Down Expand Up @@ -390,7 +389,7 @@ func (t *fanoutTestSuite) TestServerCount() {
c1 := NewClient(s1.addr, t.network)
c2 := NewClient(s2.addr, t.network)
f := New()
f.serverSelectionPolicy = &weightedPolicy{loadFactor: []int{50, 100}}
f.serverSelectionPolicy = &weightedPolicy{loadFactor: []int{50, 100}, r: rand.New(rand.NewSource(1))}
f.net = t.network
f.from = "."
f.addClient(c1)
Expand Down
6 changes: 2 additions & 4 deletions internal/selector/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package selector

import (
"math/rand"
"time"
)

// WeightedRand selector picks elements randomly based on their weights
Expand All @@ -31,13 +30,12 @@ type WeightedRand[T any] struct {
}

// NewWeightedRandSelector inits WeightedRand by copying source values and calculating total weight
func NewWeightedRandSelector[T any](values []T, weights []int) *WeightedRand[T] {
func NewWeightedRandSelector[T any](values []T, weights []int, r *rand.Rand) *WeightedRand[T] {
wrs := &WeightedRand[T]{
values: make([]T, len(values)),
weights: make([]int, len(weights)),
totalWeight: 0,
//nolint:gosec // it's overhead to use crypto/rand here
r: rand.New(rand.NewSource(time.Now().UnixNano())),
r: r,
}
// copy the underlying array values as we're going to modify content of slices
copy(wrs.values, values)
Expand Down
6 changes: 3 additions & 3 deletions internal/selector/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ func TestWeightedRand_Pick(t *testing.T) {
}
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.NewSource(1))
r := rand.New(rand.NewSource(1))

wrs := NewWeightedRandSelector(tc.values, tc.weights, r)

actual := make([]string, 0, tc.picksCount)
for i := 0; i < tc.picksCount; i++ {
Expand Down
9 changes: 7 additions & 2 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package fanout

import "github.com/networkservicemesh/fanout/internal/selector"
import (
"math/rand"

"github.com/networkservicemesh/fanout/internal/selector"
)

type policy interface {
selector(clients []Client) clientSelector
Expand All @@ -38,9 +42,10 @@ func (p *sequentialPolicy) selector(clients []Client) clientSelector {
// weightedPolicy is used to select clients randomly based on its loadFactor (weights)
type weightedPolicy struct {
loadFactor []int
r *rand.Rand
}

// creates new weighted random selector of provided clients based on loadFactor
func (p *weightedPolicy) selector(clients []Client) clientSelector {
return selector.NewWeightedRandSelector(clients, p.loadFactor)
return selector.NewWeightedRandSelector(clients, p.loadFactor, p.r)
}
6 changes: 5 additions & 1 deletion setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package fanout

import (
"math/rand"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -173,7 +174,10 @@ func initServerSelectionPolicy(f *Fanout) error {

f.serverSelectionPolicy = &sequentialPolicy{}
if f.policyType == policyWeightedRandom {
f.serverSelectionPolicy = &weightedPolicy{loadFactor: loadFactor}
f.serverSelectionPolicy = &weightedPolicy{
loadFactor: loadFactor,
r: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}

return nil
Expand Down
Loading