1
0
Fork 0

Minor improvements to random module

master
Philip O'Toole 8 months ago
parent 0e509f8192
commit 226f8cb215

@ -1,4 +1,8 @@
## 8.18.7 (unreleased) ## 8.18.8 (unreleased)
### Implementation changes and bug fixes
- [PR #1665](https://github.com/rqlite/rqlite/pull/1665): Minor improvements to `random` module.
## 8.18.7 (February 1st 2024)
### Implementation changes and bug fixes ### Implementation changes and bug fixes
- [PR #1663](https://github.com/rqlite/rqlite/pull/1663): Remove unnecessary WAL-close during Raft snapshotting. - [PR #1663](https://github.com/rqlite/rqlite/pull/1663): Remove unnecessary WAL-close during Raft snapshotting.

@ -14,16 +14,15 @@ func init() {
r = rand.New(rand.NewSource(time.Now().UnixNano())) r = rand.New(rand.NewSource(time.Now().UnixNano()))
} }
// String returns a random string of 20 characters // String returns a random string, 20 characters long.
func String() string { func String() string {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
var output strings.Builder var output strings.Builder
chars := "abcdedfghijklmnopqrstABCDEFGHIJKLMNOP" chars := "abcdedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
random := r.Intn(len(chars)) random := r.Intn(len(chars))
randomChar := chars[random] output.WriteString(string(chars[random]))
output.WriteString(string(randomChar))
} }
return output.String() return output.String()
} }
@ -35,16 +34,16 @@ func Float64() float64 {
return r.Float64() return r.Float64()
} }
// Intn returns a random int // Intn returns a random int >=0 and < n.
func Intn(n int) int { func Intn(n int) int {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
return r.Intn(n) return r.Intn(n)
} }
// Jitter adds a little bit of randomness to a given duration. This is // Jitter returns a randomly-chosen duration between d and 2d.
// useful to prevent nodes across the cluster performing certain operations
// all at the same time.
func Jitter(d time.Duration) time.Duration { func Jitter(d time.Duration) time.Duration {
return d + time.Duration(Float64()*float64(d)) mu.Lock()
defer mu.Unlock()
return d + time.Duration(r.Float64()*float64(d))
} }

@ -7,13 +7,13 @@ import (
func Test_StringLength(t *testing.T) { func Test_StringLength(t *testing.T) {
str := String() str := String()
if len(str) != 20 { if exp, got := 20, len(str); exp != got {
t.Errorf("String() returned a string of length %d; want 20", len(str)) t.Errorf("String() returned a string of length %d; want %d", got, exp)
} }
} }
func Test_StringUniqueness(t *testing.T) { func Test_StringUniqueness(t *testing.T) {
const numStrings = 10 const numStrings = 100
strs := make(map[string]bool, numStrings) strs := make(map[string]bool, numStrings)
for i := 0; i < numStrings; i++ { for i := 0; i < numStrings; i++ {
@ -26,7 +26,7 @@ func Test_StringUniqueness(t *testing.T) {
} }
func Test_Float64Uniqueness(t *testing.T) { func Test_Float64Uniqueness(t *testing.T) {
const numFloat64s = 10 const numFloat64s = 100
floats := make(map[float64]bool, numFloat64s) floats := make(map[float64]bool, numFloat64s)
for i := 0; i < numFloat64s; i++ { for i := 0; i < numFloat64s; i++ {
@ -39,7 +39,7 @@ func Test_Float64Uniqueness(t *testing.T) {
} }
func Test_IntnUniqueness(t *testing.T) { func Test_IntnUniqueness(t *testing.T) {
const numIntns = 10 const numIntns = 100
intns := make(map[int]bool, numIntns) intns := make(map[int]bool, numIntns)
for i := 0; i < numIntns; i++ { for i := 0; i < numIntns; i++ {
@ -53,9 +53,12 @@ func Test_IntnUniqueness(t *testing.T) {
func Test_Jitter(t *testing.T) { func Test_Jitter(t *testing.T) {
for n := 0; n < 100; n++ { for n := 0; n < 100; n++ {
d := Jitter(100 * time.Millisecond) dur := 100 * time.Millisecond
if d < 100*time.Millisecond || d >= 200*time.Millisecond { lower := dur
t.Errorf("Jitter(100ms) returned a duration of %s; want between 0 and 200ms", d) upper := 2 * dur
if got := Jitter(dur); got < lower || got >= upper {
t.Errorf("Jitter(%s) returned a duration of %s; want between %s and %s",
dur, got, lower, upper)
} }
} }
} }

Loading…
Cancel
Save