feat: use random request IDs instead of sequential counter
Replace the atomic counter-based request ID generation with random int64 values using math/rand/v2. This improves unpredictability and avoids potential ID collisions across client instances or restarts. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
735b288504
commit
f8daa20ddd
9 changed files with 402 additions and 26 deletions
|
|
@ -113,18 +113,24 @@ func TestJSONRPCError_Error(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestNextRequestID_Increments(t *testing.T) {
|
||||
// Get the current counter value
|
||||
initial := nextRequestID()
|
||||
func TestNextRequestID_RandomValues(t *testing.T) {
|
||||
// Generate several IDs and verify they are varied (not sequential)
|
||||
ids := make([]int64, 10)
|
||||
for i := range ids {
|
||||
ids[i] = nextRequestID()
|
||||
}
|
||||
|
||||
// Verify increments
|
||||
for i := int64(1); i <= 5; i++ {
|
||||
got := nextRequestID()
|
||||
expected := initial + i
|
||||
if got != expected {
|
||||
t.Errorf("expected %d, got %d", expected, got)
|
||||
// Check that not all IDs are sequential (would indicate non-random behavior)
|
||||
sequential := true
|
||||
for i := 1; i < len(ids); i++ {
|
||||
if ids[i] != ids[i-1]+1 {
|
||||
sequential = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if sequential {
|
||||
t.Error("IDs appear to be sequential, expected random values")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNextRequestID_ThreadSafe(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue