Skip to content

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.

}

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