Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace math/rand with math/rand/v2 #411

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading