This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
randomness.go
167 lines (129 loc) · 3.35 KB
/
randomness.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2021 The randomness Authors. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package randomness
import (
"crypto/rand"
"log"
"math"
"math/big"
"github.com/saucelabs/customerror"
"github.com/saucelabs/sypl"
"github.com/saucelabs/sypl/fields"
"github.com/saucelabs/sypl/level"
"github.com/saucelabs/sypl/options"
)
var l = sypl.NewDefault("randomgenerator", level.Info)
var (
ErrFailedToGenerateRandomness = customerror.NewFailedToError("to generate randomness")
ErrFailedToGenerateRangeSaturated = customerror.NewFailedToError("to generate, range saturated")
ErrFailedToGenerateReachedMaxRetry = customerror.NewFailedToError("to generate, reached max retry")
ErrInvalidMax = customerror.NewInvalidError("params. `max` is less than 0")
ErrInvalidMin = customerror.NewInvalidError("params. `min` is less than 1")
ErrInvalidMinBiggerThanMax = customerror.NewInvalidError("param. Min can't be bigger than max")
)
//////
// Helpers.
//////
type Randomness struct {
CollisionFree bool
Max int
Min int
maxRetry *int
memory []int64
}
// Generate returns a random number.
func (r *Randomness) Generate() (int64, error) {
// calculate the max we will be using
bg := big.NewInt(int64(r.Max - r.Min + 1))
n, err := rand.Int(rand.Reader, bg)
if err != nil {
return 0, customerror.Wrap(ErrFailedToGenerateRandomness, err)
}
port := n.Int64() + int64(r.Min)
if r.memory == nil {
return port, nil
}
if len(r.memory) > (r.Max - r.Min) {
return 0, ErrFailedToGenerateRangeSaturated
}
for _, p := range r.memory {
if p == port {
if r.maxRetry != nil {
if *r.maxRetry == 0 {
return 0, ErrFailedToGenerateReachedMaxRetry
}
l.PrintlnWithOptions(&options.Options{
Fields: fields.Fields{"retry": *r.maxRetry},
}, level.Debug, "Retrying...")
*r.maxRetry--
}
return r.Generate()
}
}
r.memory = append(r.memory, port)
return port, nil
}
// MustGenerate is like `RandomPortGenerator`, but will panic in case of any
// error.
func (r *Randomness) MustGenerate() int64 {
n, err := r.Generate()
if err != nil {
log.Panicln(err)
}
return n
}
// GenerateMany returns an slice of `n` numbers.
func (r *Randomness) GenerateMany(n int) ([]int64, error) {
numbers := []int64{}
for i := 0; i < n; i++ {
n, err := r.Generate()
if err != nil {
return nil, err
}
numbers = append(numbers, n)
}
return numbers, nil
}
// MustGenerateMany is like `GenerateMany`, but will panic in case of any error.
func (r *Randomness) MustGenerateMany(n int) []int64 {
numbers, err := r.GenerateMany(n)
if err != nil {
log.Panicln(err)
}
return numbers
}
//////
// Factory
//////
// New is the Randomness factory.
func New(min, max, maxRetry int, collisionFree bool) (*Randomness, error) {
if min < 1 {
return nil, ErrInvalidMin
}
if max < 0 {
return nil, ErrInvalidMax
}
if max == 0 {
// TODO: Switch to `math.MaxInt`, only available in Go 1.17+.
max = math.MaxInt32
}
if max < min {
return nil, ErrInvalidMinBiggerThanMax
}
var mR *int
if maxRetry > 0 {
mR = &maxRetry
}
var memory []int64
if collisionFree {
memory = []int64{}
}
return &Randomness{
CollisionFree: collisionFree,
Max: max,
Min: min,
maxRetry: mR,
memory: memory,
}, nil
}