mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Conn.Close takes context
This commit is contained in:
parent
2f948c5249
commit
3e87a8b363
6
conn.go
6
conn.go
@ -199,7 +199,7 @@ func connect(ctx context.Context, config *ConnConfig, connInfo *pgtype.ConnInfo)
|
|||||||
if c.ConnInfo == minimalConnInfo {
|
if c.ConnInfo == minimalConnInfo {
|
||||||
err = c.initConnInfo()
|
err = c.initConnInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Close()
|
c.Close(ctx)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -435,7 +435,7 @@ func (c *Conn) LocalAddr() (net.Addr, error) {
|
|||||||
|
|
||||||
// Close closes a connection. It is safe to call Close on a already closed
|
// Close closes a connection. It is safe to call Close on a already closed
|
||||||
// connection.
|
// connection.
|
||||||
func (c *Conn) Close() error {
|
func (c *Conn) Close(ctx context.Context) error {
|
||||||
c.mux.Lock()
|
c.mux.Lock()
|
||||||
defer c.mux.Unlock()
|
defer c.mux.Unlock()
|
||||||
|
|
||||||
@ -444,7 +444,7 @@ func (c *Conn) Close() error {
|
|||||||
}
|
}
|
||||||
c.status = connStatusClosed
|
c.status = connStatusClosed
|
||||||
|
|
||||||
err := c.pgConn.Close(context.TODO())
|
err := c.pgConn.Close(ctx)
|
||||||
c.causeOfDeath = errors.New("Closed")
|
c.causeOfDeath = errors.New("Closed")
|
||||||
if c.shouldLog(LogLevelInfo) {
|
if c.shouldLog(LogLevelInfo) {
|
||||||
c.log(LogLevelInfo, "closed connection", nil)
|
c.log(LogLevelInfo, "closed connection", nil)
|
||||||
|
@ -72,7 +72,7 @@ func TestConnect(t *testing.T) {
|
|||||||
t.Errorf("Did not connect as specified user (%v)", config.Config.User)
|
t.Errorf("Did not connect as specified user (%v)", config.Config.User)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = conn.Close()
|
err = conn.Close(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to close connection")
|
t.Fatal("Unable to close connection")
|
||||||
}
|
}
|
||||||
@ -481,7 +481,7 @@ func TestFatalRxError(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
otherConn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
|
otherConn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
defer otherConn.Close()
|
defer otherConn.Close(context.Background())
|
||||||
|
|
||||||
if _, err := otherConn.Exec(context.Background(), "select pg_terminate_backend($1)", conn.PID()); err != nil {
|
if _, err := otherConn.Exec(context.Background(), "select pg_terminate_backend($1)", conn.PID()); err != nil {
|
||||||
t.Fatalf("Unable to kill backend PostgreSQL process: %v", err)
|
t.Fatalf("Unable to kill backend PostgreSQL process: %v", err)
|
||||||
@ -504,7 +504,7 @@ func TestFatalTxError(t *testing.T) {
|
|||||||
defer closeConn(t, conn)
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
otherConn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
|
otherConn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
defer otherConn.Close()
|
defer otherConn.Close(context.Background())
|
||||||
|
|
||||||
_, err := otherConn.Exec(context.Background(), "select pg_terminate_backend($1)", conn.PID())
|
_, err := otherConn.Exec(context.Background(), "select pg_terminate_backend($1)", conn.PID())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -40,7 +40,7 @@ func mustReplicationConnect(t testing.TB, config pgx.ConnConfig) *pgx.Replicatio
|
|||||||
}
|
}
|
||||||
|
|
||||||
func closeConn(t testing.TB, conn *pgx.Conn) {
|
func closeConn(t testing.TB, conn *pgx.Conn) {
|
||||||
err := conn.Close()
|
err := conn.Close(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("conn.Close unexpectedly failed: %v", err)
|
t.Fatalf("conn.Close unexpectedly failed: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package pool
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgconn"
|
"github.com/jackc/pgconn"
|
||||||
"github.com/jackc/pgx"
|
"github.com/jackc/pgx"
|
||||||
@ -20,7 +21,11 @@ func Connect(ctx context.Context, connString string) (*Pool, error) {
|
|||||||
maxConnections := 5 // TODO - unhard-code
|
maxConnections := 5 // TODO - unhard-code
|
||||||
p.p = puddle.NewPool(
|
p.p = puddle.NewPool(
|
||||||
func(ctx context.Context) (interface{}, error) { return pgx.Connect(ctx, connString) },
|
func(ctx context.Context) (interface{}, error) { return pgx.Connect(ctx, connString) },
|
||||||
func(value interface{}) { value.(*pgx.Conn).Close() },
|
func(value interface{}) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
value.(*pgx.Conn).Close(ctx)
|
||||||
|
cancel()
|
||||||
|
},
|
||||||
maxConnections)
|
maxConnections)
|
||||||
|
|
||||||
// Initially establish one connection
|
// Initially establish one connection
|
||||||
|
@ -160,7 +160,7 @@ func TestConnReleaseDestroysClosedConn(t *testing.T) {
|
|||||||
c, err := pool.Acquire(ctx)
|
c, err := pool.Acquire(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
c.Conn().Close()
|
c.Conn().Close(ctx)
|
||||||
|
|
||||||
assert.Equal(t, 1, pool.Stat().TotalConns())
|
assert.Equal(t, 1, pool.Stat().TotalConns())
|
||||||
|
|
||||||
|
@ -203,7 +203,7 @@ func (rc *ReplicationConn) SendStandbyStatus(k *StandbyStatus) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *ReplicationConn) Close() error {
|
func (rc *ReplicationConn) Close() error {
|
||||||
return rc.c.Close()
|
return rc.c.Close(context.TODO())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *ReplicationConn) IsAlive() bool {
|
func (rc *ReplicationConn) IsAlive() bool {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user