Skip to content

Commit

Permalink
Replace math/rand with math/rand/v2 (#411)
Browse files Browse the repository at this point in the history
- Fixes: #342
  • Loading branch information
komuw authored Feb 9, 2024
1 parent ed0f7fd commit 2fb1649
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Most recent version is listed first.
# v0.0.91
- Upgrade to Go v1.22: https://github.com/komuw/ong/pull/408
https://github.com/komuw/ong/pull/409
- Replace math/rand with math/rand/v2: https://github.com/komuw/ong/pull/411

# v0.0.90
- ong/middleware: a http subdomain should be redirected to the same subdomain at https: https://github.com/komuw/ong/pull/406
Expand Down
4 changes: 2 additions & 2 deletions internal/acme/acme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"io"
"log/slog"
"math/big"
mathRand "math/rand"
mathRand "math/rand/v2"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -27,7 +27,7 @@ import (

// getDomain returns a valid unique domain.
func getDomain() string {
r := mathRand.Intn(100_000) + 1
r := mathRand.IntN(100_000) + 1
return fmt.Sprintf("some-sample-%d-domain.com", r)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/tst/tst.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package tst
import (
"fmt"
"math"
"math/rand"
"math/rand/v2"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -65,7 +65,7 @@ func GetPort() uint16 {

fallback:
{
r := rand.Intn(10_000) + 1
r := rand.IntN(10_000) + 1
p := math.MaxUint16 - uint16(r)
return p
}
Expand Down
12 changes: 6 additions & 6 deletions log/log_benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"io"
"log/slog"
"math/rand"
"math/rand/v2"
"testing"
"time"

Expand Down Expand Up @@ -179,7 +179,7 @@ func BenchmarkAverageCase(b *testing.B) {
b.ResetTimer()
for range b.N {
l.Info(str)
if rand.Intn(100) >= 99 {
if rand.IntN(100) >= 99 {
l.Error(logErr.Error())
}
}
Expand All @@ -191,7 +191,7 @@ func BenchmarkAverageCase(b *testing.B) {
b.ResetTimer()
for range b.N {
l.Info(str)
if rand.Intn(100) >= 99 {
if rand.IntN(100) >= 99 {
l.Error(logErr.Error())
}
}
Expand All @@ -203,7 +203,7 @@ func BenchmarkAverageCase(b *testing.B) {
b.ResetTimer()
for range b.N {
l.Info().Msg(str)
if rand.Intn(100) >= 99 {
if rand.IntN(100) >= 99 {
l.Error().Msg(logErr.Error())
}
}
Expand All @@ -215,7 +215,7 @@ func BenchmarkAverageCase(b *testing.B) {
b.ResetTimer()
for range b.N {
l.Info(sl[0], slAny...)
if rand.Intn(100) >= 99 {
if rand.IntN(100) >= 99 {
l.Error("some-error", logErr)
}
}
Expand All @@ -227,7 +227,7 @@ func BenchmarkAverageCase(b *testing.B) {
b.ResetTimer()
for range b.N {
l.Info(sl[0], slAny...)
if rand.Intn(100) >= 99 {
if rand.IntN(100) >= 99 {
l.Error("some-error", logErr)
}
}
Expand Down
4 changes: 2 additions & 2 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"fmt"
"log/slog"
mathRand "math/rand"
mathRand "math/rand/v2"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -532,7 +532,7 @@ func TestLogger(t *testing.T) {
l.Info("four" + t)
xl.Info("okay-" + t)

if mathRand.Intn(100) > 75 { // log errors 25% of the time.
if mathRand.IntN(100) > 75 { // log errors 25% of the time.
l.Error("hey", "err", errors.New("some-err-"+t))
xl.Error("some-xl-error", "err", errors.New("other-err-"+t))
}
Expand Down
4 changes: 2 additions & 2 deletions middleware/loadshed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package middleware

import (
"fmt"
mathRand "math/rand"
mathRand "math/rand/v2"
"net/http"
"slices"
"strings"
Expand Down Expand Up @@ -79,7 +79,7 @@ func loadShedder(
// Even if the server is overloaded, we want to send a percentage of the requests through.
// These requests act as a probe. If the server eventually recovers,
// these requests will re-populate latencyQueue(`lq`) with lower latencies and thus end the load-shed.
sendProbe = mathRand.Intn(100) == 1 // let 1% of requests through. NB: Intn(100) is `0-99` ie, 100 is not included.
sendProbe = mathRand.IntN(100) == 1 // let 1% of requests through. NB: Intn(100) is `0-99` ie, 100 is not included.
}

p99 := lq.getP99(loadShedMinSampleSize)
Expand Down
4 changes: 2 additions & 2 deletions middleware/loadshed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package middleware
import (
"fmt"
"io"
"math/rand"
"math/rand/v2"
"net/http"
"net/http/httptest"
"strconv"
Expand Down Expand Up @@ -253,7 +253,7 @@ func TestLatencyQueue(t *testing.T) {

func loadShedderBenchmarkHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
latency := time.Duration(rand.Intn(100)+1) * time.Millisecond
latency := time.Duration(rand.IntN(100)+1) * time.Millisecond
time.Sleep(latency)
fmt.Fprint(w, "hey")
}
Expand Down
4 changes: 2 additions & 2 deletions middleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io"
"log/slog"
mathRand "math/rand"
mathRand "math/rand/v2"
"net"
"net/http"
"time"
Expand Down Expand Up @@ -67,7 +67,7 @@ func logger(
if (lrw.code == http.StatusServiceUnavailable || lrw.code == http.StatusTooManyRequests) && w.Header().Get(retryAfterHeader) != "" {
// We are either in load shedding or rate-limiting.
// Only log (rateShedSamplePercent)% of the errors.
shouldLog := mathRand.Intn(100) <= rateShedSamplePercent
shouldLog := mathRand.IntN(100) <= rateShedSamplePercent
if shouldLog {
reqL.Error(msg, flds...)
}
Expand Down

0 comments on commit 2fb1649

Please sign in to comment.