Skip to content

Commit 55c71e6

Browse files
authored
chore: replace deprecated api rand.Seed (#1363)
### Motivation The Go 1.20 release deprecated the `rand.Seed` function, noting that programs should use the `rand.New(rand.NewSource(seed))` method instead. This change was made because using `rand.Seed` affects the global random number generator, which can lead to unexpected behavior in concurrent programs. By updating to the recommended API, we improve the code's forward compatibility and follow Go's best practices for random number generation. ### Modifications Replaced all occurrences of `rand.Seed(time.Now().UnixNano())` with `rand.New(rand.NewSource(time.Now().UnixNano()))` Updated variable assignments to use the new random generator instance where applicable Maintained the same seed behavior (using current nanosecond time) but now with proper instance isolation Verifying this change Make sure that the change passes the CI checks. This change is a trivial rework / code cleanup without any test coverage. The modification doesn't change the logical behavior of the code, only updates it to use the non-deprecated API. --------- Signed-off-by: Young Xu <[email protected]>
1 parent f9363eb commit 55c71e6

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

pulsar/backoff/backoff.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ import (
2222
"time"
2323
)
2424

25-
func init() {
26-
rand.Seed(time.Now().UnixNano())
27-
}
28-
2925
// Policy parameterize the following options in the reconnection logic to
3026
// allow users to customize the reconnection logic (minBackoff, maxBackoff and jitterPercentage)
3127
type Policy interface {
@@ -43,10 +39,11 @@ type Policy interface {
4339
// It uses an exponential backoff with jitter. The jitter represents up to 20 percents of the delay.
4440
type DefaultBackoff struct {
4541
backoff time.Duration
42+
rnd *rand.Rand
4643
}
4744

4845
func NewDefaultBackoff() Policy {
49-
return &DefaultBackoff{}
46+
return &DefaultBackoff{rnd: rand.New(rand.NewSource(time.Now().UnixNano()))}
5047
}
5148
func NewDefaultBackoffWithInitialBackOff(backoff time.Duration) Policy {
5249
return &DefaultBackoff{backoff: backoff / 2}
@@ -66,7 +63,7 @@ func (b *DefaultBackoff) Next() time.Duration {
6663
} else if b.backoff.Nanoseconds() > maxBackoff.Nanoseconds() {
6764
b.backoff = maxBackoff
6865
}
69-
jitter := rand.Float64() * float64(b.backoff) * jitterPercentage
66+
jitter := b.rnd.Float64() * float64(b.backoff) * jitterPercentage
7067

7168
return b.backoff + time.Duration(jitter)
7269
}

pulsar/internal/service_name_resolver.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,12 @@ type pulsarServiceNameResolver struct {
4343
CurrentIndex int32
4444
AddressList []*url.URL
4545

46+
rnd *rand.Rand
4647
mutex sync.Mutex
4748
}
4849

49-
func init() {
50-
rand.Seed(time.Now().UnixNano())
51-
}
52-
5350
func NewPulsarServiceNameResolver(url *url.URL) ServiceNameResolver {
54-
r := &pulsarServiceNameResolver{}
51+
r := &pulsarServiceNameResolver{rnd: rand.New(rand.NewSource(time.Now().UnixNano()))}
5552
err := r.UpdateServiceURL(url)
5653
if err != nil {
5754
log.Errorf("create pulsar service name resolver failed : %v", err)
@@ -111,7 +108,7 @@ func (r *pulsarServiceNameResolver) UpdateServiceURL(u *url.URL) error {
111108
r.AddressList = addresses
112109
r.ServiceURL = u
113110
r.ServiceURI = uri
114-
r.CurrentIndex = int32(rand.Intn(len(addresses)))
111+
r.CurrentIndex = int32(r.rnd.Intn(len(addresses)))
115112
return nil
116113
}
117114

0 commit comments

Comments
 (0)