Skip to content

Commit

Permalink
Use a random range to avoid quick checks
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Dec 6, 2024
1 parent 9a7741a commit 2954726
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fronted.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (f *fronted) findWorkingFronts() {
case <-f.stopCh:
log.Debug("findWorkingFronts::Stopping parallel dialing")
return
case <-time.After(time.Duration(rand.IntN(12000)) * time.Millisecond):
case <-time.After(time.Duration(randRange(6, 12)) * time.Second):
// Run again after a random time between 0 and 12 seconds
}
}
Expand Down Expand Up @@ -623,3 +623,7 @@ func cloneRequestWith(req *http.Request, frontedHost string, body io.ReadCloser)
}
return r, nil
}

func randRange(min, max int) int {
return rand.IntN(max-min) + min
}
22 changes: 22 additions & 0 deletions fronted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,28 @@ func TestLoadFronts(t *testing.T) {
}
}

func TestRandRange(t *testing.T) {
tests := []struct {
min, max int
}{
{1, 10},
{5, 15},
{0, 100},
{-10, 10},
{50, 60},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("min=%d,max=%d", tt.min, tt.max), func(t *testing.T) {
for i := 0; i < 100; i++ {
result := randRange(tt.min, tt.max)
assert.GreaterOrEqual(t, result, tt.min)
assert.Less(t, result, tt.max)
}
})
}
}

// Generate a mock of a MasqueradeInterface with a Dial method that can optionally
// return an error after a specified number of milliseconds.
func newMockFront(domain string, ipAddress string, timeout time.Duration, passesCheck bool) *mockFront {
Expand Down

0 comments on commit 2954726

Please sign in to comment.