diff --git a/internal/udp/reliability.go b/internal/udp/reliability.go index be2c604..a919229 100644 --- a/internal/udp/reliability.go +++ b/internal/udp/reliability.go @@ -95,10 +95,7 @@ func (rq *RetransmitQueue) GetExpired() []*RetransmitEntry { // calculateTimeout computes timeout with exponential backoff func (rq *RetransmitQueue) calculateTimeout(attempts int) time.Duration { - timeout := rq.baseTimeout * time.Duration(attempts*attempts) // Quadratic backoff - if timeout > rq.maxTimeout { - timeout = rq.maxTimeout - } + timeout := min(rq.baseTimeout*time.Duration(attempts*attempts), rq.maxTimeout) // Quadratic backoff return timeout } @@ -169,10 +166,7 @@ func (fm *FragmentManager) FragmentPacket(data []byte, startSeq uint16) []*Proto seq := startSeq for offset := 0; offset < len(data); offset += chunkSize { - end := offset + chunkSize - if end > len(data) { - end = len(data) - } + end := min(offset+chunkSize, len(data)) var fragmentData []byte if offset == 0 { diff --git a/internal/udp/udp_test.go b/internal/udp/udp_test.go index fa742a2..3edee13 100644 --- a/internal/udp/udp_test.go +++ b/internal/udp/udp_test.go @@ -390,8 +390,7 @@ func BenchmarkCRC(b *testing.B) { data[i] = byte(i) } - b.ResetTimer() - for range b.N { + for b.Loop() { CalculateCRC32(data) } } @@ -403,8 +402,7 @@ func BenchmarkCompression(b *testing.B) { data[i] = byte(i % 256) } - b.ResetTimer() - for range b.N { + for b.Loop() { compressed, _ := Compress(data) Decompress(compressed) } @@ -420,8 +418,7 @@ func BenchmarkEncryption(b *testing.B) { data[i] = byte(i) } - b.ResetTimer() - for range b.N { + for b.Loop() { encrypted := crypto.Encrypt(data) crypto.Decrypt(encrypted) }