go.temporal.io/server/common/fastrand/fastrand.go

172 LOC · 16 covered · 156 uncovered · 5 ranges · 27 concepts · 2 introducers · 16 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filego.temporal.io/server/common/stats/windowed_tdigest.go · 244 LOCstats/windowed_tdigest.g…health_signal_aggregator.go ×1 · 1 introduced LOChealth_signal_aggregator…deep_health_check.go ×1 · 2 introduced LOCdeep_health_check.go ×1all_checks_healthy · 0 introduced LOCall_checks_healthyinit_window_does_not_suppress_threshold_checks · 0 introduced LOCinit_window_does_not_sup…deep_health_check.go ×1 · 2 introduced LOCdeep_health_check.go ×1deep_health_check.go ×1 · 2 introduced LOCdeep_health_check.go ×1deep_health_check.go ×1 · 2 introduced LOCdeep_health_check.go ×1moving_window_average.go ×2 · 7 introduced LOCmoving_window_average.go…windowed_tdigest.go ×1 · 2 introduced LOCwindowed_tdigest.go ×1windowed_tdigest.go ×1 · 1 introduced LOCwindowed_tdigest.go ×1TestWindowedDigest_MultipleValuesInSameWindow · 0 introduced LOCTestWindowedDigest_Multi…TestWindowedDigest_RingBufferWrapPreservesNewest · 0 introduced LOCTestWindowedDigest_RingB…blank-drops-old-data · 0 introduced LOCblank-drops-old-datafill-blanks-simple · 0 introduced LOCfill-blanks-simpleTestWindowedDigest_RecordToLatestWindow · 0 introduced LOCTestWindowedDigest_Recor…overflow · 0 introduced LOCoverflowwindowed_tdigest.go ×2 · 13 introduced LOCwindowed_tdigest.go ×2windowed_tdigest.go ×2 · 6 introduced LOCwindowed_tdigest.go ×2TestWindowedDigest_RecordMultiWeighted · 0 introduced LOCTestWindowedDigest_Recor…windowed_tdigest.go ×2 · 6 introduced LOCwindowed_tdigest.go ×2windowed_tdigest.go ×1 · 5 introduced LOCwindowed_tdigest.go ×1windowed_tdigest.go ×1 · 3 introduced LOCwindowed_tdigest.go ×1windowed_tdigest.go ×1 · 1 introduced LOCwindowed_tdigest.go ×1windowed_tdigest.go ×1 · 1 introduced LOCwindowed_tdigest.go ×1windowed_tdigest.go ×1 · 2 introduced LOCwindowed_tdigest.go ×1fastrand.go ×1 · 6 introduced LOCfastrand.go ×1fastrand.go ×4 · 13 introduced LOCfastrand.go ×4TestBasicRandomness · introduced test · go.temporal.io/server/common/fastrand/TestBasicRandomnessTestBasicRandomnessblank-drops-old-data · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest/blank-drops-old-datablank-drops-old-datafill-blanks-simple · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest/fill-blanks-simplefill-blanks-simpleoverflow · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest/overflowoverflowTestWindowedDigest_GapTimestampDropped · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest_GapTimestampDroppedTestWindowedDigest_GapTi…TestWindowedDigest_MultipleValuesInSameWindow · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest_MultipleValuesInSameWindowTestWindowedDigest_Multi…TestWindowedDigest_OldTimestampDropped · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest_OldTimestampDroppedTestWindowedDigest_OldTi…TestWindowedDigest_RecordMultiWeighted · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest_RecordMultiWeightedTestWindowedDigest_Recor…TestWindowedDigest_RecordToLatestWindow · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest_RecordToLatestWindowTestWindowedDigest_Recor…TestWindowedDigest_RingBufferWrapPreservesNewest · introduced test · go.temporal.io/server/common/stats/TestWindowedDigest_RingBufferWrapPreservesNewestTestWindowedDigest_RingB…all_checks_healthy · introduced test · go.temporal.io/server/service/history/TestDeepHealthCheck/all_checks_healthyall_checks_healthygrpc_not_serving_propagates_after_init_window_expires · introduced test · go.temporal.io/server/service/history/TestDeepHealthCheck/grpc_not_serving_propagates_after_init_window_expiresgrpc_not_serving_propaga…grpc_not_serving_suppressed_within_init_window · introduced test · go.temporal.io/server/service/history/TestDeepHealthCheck/grpc_not_serving_suppressed_within_init_windowgrpc_not_serving_suppres…init_window_does_not_suppress_threshold_checks · introduced test · go.temporal.io/server/service/history/TestDeepHealthCheck/init_window_does_not_suppress_threshold_checksinit_window_does_not_sup…rpc_latency_and_persistence_error_ratio_over_thresholds · introduced test · go.temporal.io/server/service/history/TestDeepHealthCheck/rpc_latency_and_persistence_error_ratio_over_thresholdsrpc_latency_and_persiste…rpc_latency_and_persistence_error_ratio_over_thresholds,_percentiles_enforced · introduced test · go.temporal.io/server/service/history/TestDeepHealthCheck/rpc_latency_and_persistence_error_ratio_over_thresholds,_percentiles_enforcedrpc_latency_and_persiste…Focused file · go.temporal.io/server/common/fastrand/fastrand.go · 172 LOCfastrand/fastrand.go

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 // Package fastrand exposes an object [Rand] which can be used as a simple
2 // drop-in replacement for `[math/rand.Rand]` where performance or thread
3 // safety is required.
4 package fastrand
5
6 import (
7 "math/rand"
8 "sync"
9
10 "github.com/caio/go-tdigest/v5"
11 )
12
13 // globalRngPool is a globally shared object for allowing lock-free reuse
14 // of shared random number generators. In practice we would not expect this
15 // pool to contain many more objects than the number of CPU cores running
16 // the code.
17 var globalRngPool = sync.Pool{
18 > New: func() any { fastrand.go ×4
19 > return rand.New(rand.NewSource(rand.Int63()))
20 > },
21 }
22
23 // getRng returns a thread-local [math/rand.Rand] object, largely a wrapper
24 // for globalRngPool.Get(), with a typecast and assertion that everything is
25 // as expected.
26 > func getRng() *rand.Rand { fastrand.go ×4
27 > rng, ok := globalRngPool.Get().(*rand.Rand)
28 > if !ok {
29 // nolint:forbidigo
30 panic("Failed to access valid object from globalRngPool") // This should never happen, since it would mean someone put an invalid object into the pool.
31 }
32
33 > return rng fastrand.go ×4
34 }
35
36 // Rand is an object that behaves largely as-if it was a [math/rand.Rand],
37 // with the key distinction that it is thread-safe and highly performant.
38 //
39 // Under the hood this uses a thread-safe pool of [math/rand.Rand] objects
40 // which it will dynamically create and access for each call. As a result,
41 // this does not support setting the seed, since the underlying objects
42 // are ephemeral.
43 type Rand struct{}
44
45 // ExpFloat64 implements [math/rand.Rand.ExpFloat64].
46 func (r Rand) ExpFloat64() float64 {
47 rng := getRng()
48 res := rng.ExpFloat64()
49 globalRngPool.Put(rng)
50 return res
51 }
52
53 // Float32 implements [math/rand.Rand.Float32].
54 func (r Rand) Float32() float32 {
55 rng := getRng()
56 res := rng.Float32()
57 globalRngPool.Put(rng)
58 return res
59 }
60
61 // Float64 implements [math/rand.Rand.Float64].
62 func (r Rand) Float64() float64 {
63 rng := getRng()
64 res := rng.Float64()
65 globalRngPool.Put(rng)
66 return res
67 }
68
69 // Int implements [math/rand.Rand.Int].
70 func (r Rand) Int() int {
71 rng := getRng()
72 res := rng.Int()
73 globalRngPool.Put(rng)
74 return res
75 }
76
77 // Int31 implements [math/rand.Rand.Int31].
78 func (r Rand) Int31() int32 {
79 rng := getRng()
80 res := rng.Int31()
81 globalRngPool.Put(rng)
82 return res
83 }
84
85 // Int31n implements [math/rand.Rand.Int31n].
86 func (r Rand) Int31n(n int32) int32 {
87 rng := getRng()
88 res := rng.Int31n(n)
89 globalRngPool.Put(rng)
90 return res
91 }
92
93 // Int63 implements [math/rand.Rand.Int63].
94 func (r Rand) Int63() int64 {
95 rng := getRng()
96 res := rng.Int63()
97 globalRngPool.Put(rng)
98 return res
99 }
100
101 // Int63n implements [math/rand.Rand.Int63n].
102 func (r Rand) Int63n(n int64) int64 {
103 rng := getRng()
104 res := rng.Int63n(n)
105 globalRngPool.Put(rng)
106 return res
107 }
108
109 // Intn implements [math/rand.Rand.Intn].
110 > func (r Rand) Intn(n int) int { fastrand.go ×4
111 > rng := getRng()
112 > res := rng.Intn(n)
113 > globalRngPool.Put(rng)
114 > return res
115 > }
116
117 // NormFloat64 implements [math/rand.Rand.NormFloat64].
118 func (r Rand) NormFloat64() float64 {
119 rng := getRng()
120 res := rng.NormFloat64()
121 globalRngPool.Put(rng)
122 return res
123 }
124
125 // Perm implements [math/rand.Rand.Perm].
126 func (r Rand) Perm(n int) []int {
127 rng := getRng()
128 res := rng.Perm(n)
129 globalRngPool.Put(rng)
130 return res
131 }
132
133 // Read implements [math/rand.Rand.Read].
134 func (r Rand) Read(p []byte) (n int, err error) {
135 rng := getRng()
136 res, err := rng.Read(p)
137 globalRngPool.Put(rng)
138 return res, err
139 }
140
141 // Seed implements [math/rand.Rand.Seed].
142 func (r Rand) Seed(seed int64) {
143 // Do nothing, setting seeds is not supported since you may always get a different underlying rng.
144 }
145
146 // Shuffle [math/rand.Rand.Shuffle].
147 func (r Rand) Shuffle(n int, swap func(i int, j int)) {
148 rng := getRng()
149 rng.Shuffle(n, swap)
150 globalRngPool.Put(rng)
151 }
152
153 // Uint32 implements [math/rand.Rand.Uint32].
154 func (r Rand) Uint32() uint32 {
155 rng := getRng()
156 res := rng.Uint32()
157 globalRngPool.Put(rng)
158 return res
159 }
160
161 // Uint64 implements [math/rand.Rand.Uint64].
162 func (r Rand) Uint64() uint64 {
163 rng := getRng()
164 res := rng.Uint64()
165 globalRngPool.Put(rng)
166 return res
167 }
168
169 // Clone method is a no-op to support use in tdigest library.
170 > func (r Rand) Clone() tdigest.RNG { fastrand.go ×1
171 > return r
172 > }