diff --git a/pgxpool/pool.go b/pgxpool/pool.go new file mode 100644 index 0000000..7d13318 --- /dev/null +++ b/pgxpool/pool.go @@ -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 +} \ No newline at end of file