Skip to content
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
44 changes: 44 additions & 0 deletions pgxpool/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pgxpool

import (
"context"
"sync"
"sync/atomic"
"github.com/jackc/puddle/v2"
)

type Pool struct {
puddle *puddle.Pool
mu sync.Mutex
pendingConnections int32
maxConns int32
// ... other fields
}

func (p *Pool) Acquire(ctx context.Context) (*Conn, error) {
// Logic to check pending connections before creating new ones
for {
p.mu.Lock()
if p.pendingConnections + p.getCurrentActive() < p.maxConns {
p.pendingConnections++
p.mu.Unlock()
break
}
p.mu.Unlock()
// Wait for a slot or context cancellation
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
// Wait logic here
}
}

// Perform connection creation
conn, err := p.createConn(ctx)

// Always decrement pending connections
atomic.AddInt32(&p.pendingConnections, -1)

return conn, err
}