diff --git a/fronted.go b/fronted.go index 7cb6ed0..d717eeb 100644 --- a/fronted.go +++ b/fronted.go @@ -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 } } @@ -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 +} diff --git a/fronted_test.go b/fronted_test.go index e1a5b21..2ac5fb0 100644 --- a/fronted_test.go +++ b/fronted_test.go @@ -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 {