Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fix rand.Seed() duplicate concurrent calls #1958

Merged
merged 3 commits into from
Jul 16, 2022

Conversation

CoolIceV
Copy link
Contributor

@CoolIceV CoolIceV commented Jul 7, 2022

What this PR does:
Move the rand.Seed() call to init()

Which issue(s) this PR fixes:
Fixes #1957

You should pay attention to items below to ensure your pr passes our ci test
We do not merge pr with ci tests failed

  • All ut passed (run 'go test ./...' in project root)
  • After go-fmt ed , run 'go fmt project' using goland.
  • Golangci-lint passed, run 'sudo golangci-lint run' in project root.
  • After import formatted, (using imports-formatter to run 'imports-formatter .' in project root, to format your import blocks, mentioned in CONTRIBUTING.md above)
  • Your new-created file needs to have apache license at the top, like other existed file does.
  • All integration test passed. You can run integration test locally (with docker env). Clone our dubbo-go-samples project and replace the go.mod to your dubbo-go, and run 'sudo sh start_integration_test.sh' at root of samples project root. (M1 Slice is not Support)

@@ -44,6 +44,7 @@ var (
)

func init() {
rand.Seed(randSeed())
Copy link
Member

@binbin0325 binbin0325 Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rand.Seed only need to initialize once

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think there should be a sync.Once to initialize the seed globally. Did dubbo-go have it before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each init() is called only once in golang, and I think it is sufficient to ensure that the seed does not change after the program starts running, regardless of the number of times the rand.Seed() is called in the program.

In addition, the imported third-party package may call rand.Seed() even if we only run it once

Copy link
Member

@justxuewei justxuewei Jul 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the number of initializations of the rand seed is not a very strict limit. This PR looks good for me. BTW, I am saying that initialize the rand seed globally, instead of initialize that in init() function of each package, but it doesn't matter now.

@codecov-commenter
Copy link

Codecov Report

Merging #1958 (ed6feb5) into 3.0 (026711b) will decrease coverage by 0.60%.
The diff coverage is 27.27%.

@@            Coverage Diff             @@
##              3.0    #1958      +/-   ##
==========================================
- Coverage   44.96%   44.35%   -0.61%     
==========================================
  Files         287      283       -4     
  Lines       17135    17023     -112     
==========================================
- Hits         7704     7551     -153     
- Misses       8618     8668      +50     
+ Partials      813      804       -9     
Impacted Files Coverage Δ
cluster/cluster/base/cluster_invoker.go 24.44% <ø> (ø)
cluster/cluster/broadcast/cluster_invoker.go 80.00% <ø> (ø)
cluster/cluster/failback/cluster_invoker.go 75.82% <ø> (ø)
cluster/cluster/failover/cluster_invoker.go 66.66% <ø> (ø)
cluster/cluster/failsafe/cluster_invoker.go 81.81% <ø> (ø)
cluster/cluster/forking/cluster_invoker.go 58.33% <ø> (ø)
cluster/loadbalance/ringhash/ringhash.go 63.33% <ø> (ø)
cluster/router/tag/match.go 90.69% <ø> (ø)
cluster/router/tag/router.go 59.64% <ø> (ø)
common/extension/proxy_factory.go 0.00% <ø> (ø)
... and 131 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1abbc2e...ed6feb5. Read the comment docs.

@AlexStocks AlexStocks merged commit 27d68ab into apache:3.0 Jul 16, 2022
@CoolIceV CoolIceV deleted the fix/randseed branch August 9, 2022 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants