-
Notifications
You must be signed in to change notification settings - Fork 0
/
powerof2.go
51 lines (44 loc) · 1.23 KB
/
powerof2.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
package sample
import (
"errors"
"fmt"
"math/rand"
"time"
)
// PowerOf2 is a Sampler, which only works for rates being a power of 2.
// The sampling is done by the calculation of the remainder:
// probe & (rate - 1) == 0
type powerOf2 struct {
sampleState
}
// NewPowerOf2 returns a PowerOf2 Sampler, random number generator is seeded with UnixNano
func NewPowerOf2(rate uint64) (Sampler, error) {
return NewPowerOf2Seeded(rate, time.Now().UTC().UnixNano())
}
// NewPowerOf2Seeded returns a PowerOf2 Sampler, allow for manual seeding of the random number generator
func NewPowerOf2Seeded(rate uint64, seed int64) (Sampler, error) {
if rate == 0 {
return nil, errors.New("rate must not be 0")
}
if rate&(rate-1) != 0 {
return nil, errors.New("rate must be a power of 2")
}
rnd := rand.New(rand.NewSource(seed))
return &powerOf2{sampleState: sampleState{rate: rate, seed: seed, rnd: rnd}}, nil
}
func (po2 *powerOf2) Sample() bool {
return po2.SampleFrom(randUint64(po2.rnd))
}
func (po2 *powerOf2) SampleFrom(probe uint64) bool {
po2.sampleCount++
if probe&(po2.rate-1) == 0 {
po2.trueCount++
return true
}
return false
}
func (po2 *powerOf2) String() string {
type X *powerOf2
x := X(po2)
return fmt.Sprintf("%+v", x)
}