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

Faster shutdown of pgxpool.Pool background goroutines #1642

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
32 changes: 24 additions & 8 deletions pgxpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ type Pool struct {
maxConnIdleTime time.Duration
healthCheckPeriod time.Duration

healthCheckMu sync.Mutex
healthCheckTimer *time.Timer

healthCheckChan chan struct{}

closeOnce sync.Once
Expand Down Expand Up @@ -381,15 +384,25 @@ func (p *Pool) isExpired(res *puddle.Resource[*connResource]) bool {
}

func (p *Pool) triggerHealthCheck() {
go func() {
const healthCheckDelay = 500 * time.Millisecond

p.healthCheckMu.Lock()
defer p.healthCheckMu.Unlock()

if p.healthCheckTimer == nil {
// Destroy is asynchronous so we give it time to actually remove itself from
// the pool otherwise we might try to check the pool size too soon
time.Sleep(500 * time.Millisecond)
select {
case p.healthCheckChan <- struct{}{}:
default:
}
}()
p.healthCheckTimer = time.AfterFunc(healthCheckDelay, func() {
select {
case <-p.closeChan:
case p.healthCheckChan <- struct{}{}:
default:
}
})
return
}

p.healthCheckTimer.Reset(healthCheckDelay)
}

func (p *Pool) backgroundHealthCheck() {
Expand All @@ -408,6 +421,9 @@ func (p *Pool) backgroundHealthCheck() {
}

func (p *Pool) checkHealth() {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
Comment on lines +424 to +425
Copy link
Contributor Author

@bgentry bgentry Jun 14, 2023

Choose a reason for hiding this comment

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

I opted for a time.Ticker on this one for simplicity, but this does have the side effect of changing this behavior from a "sleep 500ms between attempts" to a "make an attempt every 500ms" type behavior. If the health checks take awhile then there could be minimal sleeping time.

I can refactor to use a timer, it would just be messier. I wanted to propose this option first and see what you think.

Copy link
Owner

Choose a reason for hiding this comment

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

I don't actually understand the purpose of this change. Aside from the wait 500ms between vs try every 500ms it doesn't change anything does it?


for {
// If checkMinConns failed we don't destroy any connections since we couldn't
// even get to minConns
Expand All @@ -424,7 +440,7 @@ func (p *Pool) checkHealth() {
select {
case <-p.closeChan:
return
case <-time.After(500 * time.Millisecond):
case <-ticker.C:
}
}
}
Expand Down