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

kes: use global math/rand instead of math/rand.New #26

Merged
merged 1 commit into from
Jan 15, 2025
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
11 changes: 1 addition & 10 deletions kes/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ type dnsResolver interface {
LookupHost(ctx context.Context, host string) (addrs []string, err error)
}

type randomNumberGenerator interface {
Intn(n int) int
}

type loadBalancer struct {
enclave string
endpoints []*loadBalancerEndpoint
rand randomNumberGenerator
DNSResolver dnsResolver
getLocalNetworks func() ([]net.Addr, error)
sendRequest func(context.Context, *endpointRequest) (*http.Response, error)
Expand All @@ -41,7 +36,6 @@ func newLoadBalancer(enclaveName string) *loadBalancer {
DNSResolver: new(net.Resolver),
getLocalNetworks: net.InterfaceAddrs,
sendRequest: sendRequest,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}

Expand Down Expand Up @@ -114,9 +108,6 @@ func (lb *loadBalancer) prepareLoadBalancer(endpoints []string) {
if lb.sendRequest == nil {
lb.sendRequest = sendRequest
}
if lb.rand == nil {
lb.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
}

ifNetworks := make(map[string]*net.IPNet)
ifs, err := lb.getLocalNetworks()
Expand Down Expand Up @@ -319,7 +310,7 @@ func (lb *loadBalancer) Send(ctx context.Context, client *retry, method string,
endpointCount = len(lb.endpoints)
tryCount = 0
fullRetry = false
R = lb.rand.Intn(len(lb.endpoints))
R = rand.Intn(len(lb.endpoints))
nextEndpointIndex int
)

Expand Down
21 changes: 0 additions & 21 deletions kes/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"errors"
"io"
"math/rand"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -164,7 +163,6 @@ func TestPrepareLoadBalancer(t *testing.T) {
enclave: "",
DNSResolver: Resolver,
getLocalNetworks: MockGetInterfaces,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}

dnsErrorEndpoint := "https://minio.404:7373"
Expand Down Expand Up @@ -223,7 +221,6 @@ func TestLoadBalancerSend_SingleHost(t *testing.T) {
DNSResolver: Resolver,
getLocalNetworks: MockGetInterfaces,
sendRequest: MockSendRequest(200, false),
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}

retryClient := new(retry)
Expand Down Expand Up @@ -287,7 +284,6 @@ func TestLoadBalancerSend_MultiHost(t *testing.T) {
DNSResolver: Resolver,
getLocalNetworks: MockGetInterfaces,
sendRequest: MockSendRequest(200, false),
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}

retryClient := new(retry)
Expand Down Expand Up @@ -378,21 +374,4 @@ func TestLoadBalancerSend_MultiHost(t *testing.T) {
for i := range lb.endpoints {
lb.endpoints[i].timeout = time.Now().AddDate(0, 0, -1)
}

// This test triggers a probe on all endpoints in order
// to clear all timeouts
mockRandom := new(MockRandomNumber)
lb.rand = mockRandom
mockRandom.Number = 0
lb.sendRequest = MockSendRequest(200, false)
for i := 0; i < len(endpoints); i++ {
_, _ = lb.Send(ctx, retryClient, method, path, nil, options...)
mockRandom.Number++
}

for _, v := range lb.endpoints {
if !v.timeout.IsZero() {
t.Fatalf("Endpoint %s was expected to NOT be timed-out", v.addr)
}
}
}
Loading