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

Remove data race. #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

dougnd
Copy link

@dougnd dougnd commented Feb 19, 2021

This should remove the races detected in #141. Let me know what you think!

Fixes #141

if p.created < p.max {
p.makeOne()
}
p.makeOne()
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this result in unlimited amount of connections?

I believe that the p.created < p.max guard should ensure we only get the specified max connections.

Copy link
Author

Choose a reason for hiding this comment

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

I believe (and correct me if I'm wrong) that makeOne should be safe to do. It spins up a goroutine that:

  • Calls inc()
    • inc() will check p.created >= p.max and if true, return false
  • If inc() returns false nothing happens

So makeOne should not create a new connection unless p.created < p.max. The benefit to letting makeOne do the logic is that it handles the concurrency better -- it locks the mutex appropriately.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just checked, you are right.

@@ -142,10 +140,6 @@ func (p *Pool) replace(c *client) {
}

func (p *Pool) inc() bool {
if p.created >= p.max {
Copy link
Contributor

Choose a reason for hiding this comment

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

Makes a lot of sense, since it is duplicated.

Choose a reason for hiding this comment

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

Is this safe to merge?

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.

Data Race in Connection Pool
3 participants