From 2954726ca423469fd493882c9a2d8b727ba1f77d Mon Sep 17 00:00:00 2001 From: Adam Fisk Date: Fri, 6 Dec 2024 13:24:33 -0700 Subject: [PATCH] Use a random range to avoid quick checks --- fronted.go | 6 +++++- fronted_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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 {