mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
Update to math/rand/v2
This commit updates the uses of math/rand to math/rand/v2, which was released in Go 1.22 (2024-02). The new package is generally safer, see https://go.dev/blog/randv2 for the details. There are no user-visible changes, it is only adjusting the name of functions, simplify some code thanks to v2 having a better API, etc.
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -52,9 +51,6 @@ func main() {
|
|||||||
|
|
||||||
log.Infof("chasquid starting (version %s)", version)
|
log.Infof("chasquid starting (version %s)", version)
|
||||||
|
|
||||||
// Seed the PRNG, just to prevent for it to be totally predictable.
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
|
|
||||||
conf, err := config.Load(*configDir+"/chasquid.conf", *configOverrides)
|
conf, err := config.Load(*configDir+"/chasquid.conf", *configOverrides)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error loading config: %v", err)
|
log.Fatalf("Error loading config: %v", err)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module blitiri.com.ar/go/chasquid
|
module blitiri.com.ar/go/chasquid
|
||||||
|
|
||||||
go 1.21
|
go 1.22
|
||||||
|
|
||||||
require (
|
require (
|
||||||
blitiri.com.ar/go/log v1.1.0
|
blitiri.com.ar/go/log v1.1.0
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand/v2"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -76,7 +76,7 @@ func (a *Authenticator) Authenticate(tr *trace.Trace, user, domain, password str
|
|||||||
delay := a.AuthDuration - elapsed
|
delay := a.AuthDuration - elapsed
|
||||||
if delay > 0 {
|
if delay > 0 {
|
||||||
maxDelta := int64(float64(delay) * 0.2)
|
maxDelta := int64(float64(delay) * 0.2)
|
||||||
delay += time.Duration(rand.Int63n(maxDelta))
|
delay += time.Duration(rand.Int64N(maxDelta))
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
}
|
}
|
||||||
}(time.Now())
|
}(time.Now())
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand/v2"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -48,23 +48,23 @@ func RandomEvents(family string) {
|
|||||||
func randomTrace(family string, tr nettrace.Trace) {
|
func randomTrace(family string, tr nettrace.Trace) {
|
||||||
tr.Printf("this is a random event")
|
tr.Printf("this is a random event")
|
||||||
tr.Printf("and it has a random delay")
|
tr.Printf("and it has a random delay")
|
||||||
delay := time.Duration(rand.Intn(1000)) * time.Millisecond
|
delay := rand.N(1000 * time.Millisecond)
|
||||||
tr.Printf("sleeping %v", delay)
|
tr.Printf("sleeping %v", delay)
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
|
|
||||||
if rand.Intn(100) < 1 {
|
if rand.IntN(100) < 1 {
|
||||||
tr.Printf("this unlucky one is an error")
|
tr.Printf("this unlucky one is an error")
|
||||||
tr.SetError()
|
tr.SetError()
|
||||||
}
|
}
|
||||||
|
|
||||||
if rand.Intn(100) < 10 {
|
if rand.IntN(100) < 10 {
|
||||||
tr.Printf("this one got super verbose!")
|
tr.Printf("this one got super verbose!")
|
||||||
for j := 0; j < 100; j++ {
|
for j := 0; j < 100; j++ {
|
||||||
tr.Printf("message %d", j)
|
tr.Printf("message %d", j)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if rand.Intn(100) < 30 {
|
if rand.IntN(100) < 30 {
|
||||||
tr.Printf("this one had a child")
|
tr.Printf("this one had a child")
|
||||||
c := tr.NewChild(family, "achild")
|
c := tr.NewChild(family, "achild")
|
||||||
go randomTrace(family, c)
|
go randomTrace(family, c)
|
||||||
@@ -78,7 +78,7 @@ func RandomLongEvent(family, title string) {
|
|||||||
tr := nettrace.New(family, title)
|
tr := nettrace.New(family, title)
|
||||||
tr.Printf("this is a random long event")
|
tr.Printf("this is a random long event")
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
delay := time.Duration(rand.Intn(100)) * time.Millisecond
|
delay := rand.N(100 * time.Millisecond)
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
tr.Printf("message %d, slept %v", i, delay)
|
tr.Printf("message %d, slept %v", i, delay)
|
||||||
}
|
}
|
||||||
@@ -90,16 +90,16 @@ func RandomBunny(family, title string) {
|
|||||||
tr.SetMaxEvents(100)
|
tr.SetMaxEvents(100)
|
||||||
tr.Printf("this is the top 🐇")
|
tr.Printf("this is the top 🐇")
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
delay := time.Duration(rand.Intn(100)) * time.Millisecond
|
delay := rand.N(100 * time.Millisecond)
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
tr.Printf("message %d, slept %v", i, delay)
|
tr.Printf("message %d, slept %v", i, delay)
|
||||||
|
|
||||||
if rand.Intn(100) < 40 {
|
if rand.IntN(100) < 40 {
|
||||||
c := tr.NewChild(family, fmt.Sprintf("child-%d", i))
|
c := tr.NewChild(family, fmt.Sprintf("child-%d", i))
|
||||||
go randomTrace(family, c)
|
go randomTrace(family, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rand.Intn(100) < 40 {
|
if rand.IntN(100) < 40 {
|
||||||
n := nettrace.New(family, fmt.Sprintf("linked-%d", i))
|
n := nettrace.New(family, fmt.Sprintf("linked-%d", i))
|
||||||
go randomTrace(family, n)
|
go randomTrace(family, n)
|
||||||
tr.Link(n, "linking with this guy")
|
tr.Link(n, "linking with this guy")
|
||||||
@@ -114,7 +114,7 @@ func randomNested(family string, depth int, parent nettrace.Trace) {
|
|||||||
|
|
||||||
tr.Printf("I am a spoiled child")
|
tr.Printf("I am a spoiled child")
|
||||||
|
|
||||||
delay := time.Duration(rand.Intn(100)) * time.Millisecond
|
delay := rand.N(100 * time.Millisecond)
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
tr.Printf("slept %v", delay)
|
tr.Printf("slept %v", delay)
|
||||||
|
|
||||||
@@ -124,12 +124,12 @@ func randomNested(family string, depth int, parent nettrace.Trace) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we make this < 50, then it grows forever.
|
// If we make this < 50, then it grows forever.
|
||||||
if rand.Intn(100) < 75 {
|
if rand.IntN(100) < 75 {
|
||||||
tr.Printf("I sang really well")
|
tr.Printf("I sang really well")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
max := rand.Intn(5)
|
max := rand.IntN(5)
|
||||||
for i := 0; i < max; i++ {
|
for i := 0; i < max; i++ {
|
||||||
tr.Printf("spawning %d", i)
|
tr.Printf("spawning %d", i)
|
||||||
go randomNested(family, depth+1, tr)
|
go randomNested(family, depth+1, tr)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ package nettrace
|
|||||||
import (
|
import (
|
||||||
"container/ring"
|
"container/ring"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand/v2"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand/v2"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -67,17 +68,11 @@ var newID chan string
|
|||||||
|
|
||||||
func generateNewIDs() {
|
func generateNewIDs() {
|
||||||
// The IDs are only used internally, we are ok with using a PRNG.
|
// The IDs are only used internally, we are ok with using a PRNG.
|
||||||
// We create our own to avoid relying on external sources initializing it
|
|
||||||
// properly.
|
|
||||||
prng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
|
|
||||||
// IDs are base64(8 random bytes), but the code doesn't care.
|
// IDs are base64(8 random bytes), but the code doesn't care.
|
||||||
buf := make([]byte, 8)
|
buf := make([]byte, 8)
|
||||||
id := ""
|
|
||||||
for {
|
for {
|
||||||
prng.Read(buf)
|
binary.NativeEndian.PutUint64(buf, rand.Uint64())
|
||||||
id = base64.RawURLEncoding.EncodeToString(buf)
|
newID <- base64.RawURLEncoding.EncodeToString(buf)
|
||||||
newID <- id
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -485,7 +480,7 @@ func nextDelay(createdAt time.Time) time.Duration {
|
|||||||
|
|
||||||
// Perturb the delay, to avoid all queued emails to be retried at the
|
// Perturb the delay, to avoid all queued emails to be retried at the
|
||||||
// exact same time after a restart.
|
// exact same time after a restart.
|
||||||
delay += time.Duration(rand.Intn(60)) * time.Second
|
delay += rand.N(60 * time.Second)
|
||||||
return delay
|
return delay
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand/v2"
|
||||||
"net"
|
"net"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"os"
|
"os"
|
||||||
|
|||||||
Reference in New Issue
Block a user