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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ func (p *Pool) get(timeout time.Duration) *client {
default:
}

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.


var deadline <-chan time.Time
if timeout >= 0 {
Expand Down Expand Up @@ -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?

return false
}

p.mut.Lock()
defer p.mut.Unlock()

Expand Down