MCPcopy
hub / github.com/jackc/pgx / Close

Method Close

pgconn/pgconn.go:738–768  ·  view source on GitHub ↗

Close closes a connection. It is safe to call Close on an already closed connection. Close attempts a clean close by sending the exit message to PostgreSQL. However, this could block so ctx is available to limit the time to wait. The underlying net.Conn.Close() will always be called regardless of an

(ctx context.Context)

Source from the content-addressed store, hash-verified

736// sending the exit message to PostgreSQL. However, this could block so ctx is available to limit the time to wait. The
737// underlying net.Conn.Close() will always be called regardless of any other errors.
738func (pgConn *PgConn) Close(ctx context.Context) error {
739 if pgConn.status == connStatusClosed {
740 return nil
741 }
742 pgConn.status = connStatusClosed
743
744 defer close(pgConn.cleanupDone)
745 defer pgConn.conn.Close()
746
747 if ctx != context.Background() {
748 // Close may be called while a cancellable query is in progress. This will most often be triggered by panic when
749 // a defer closes the connection (possibly indirectly via a transaction or a connection pool). Unwatch to end any
750 // previous watch. It is safe to Unwatch regardless of whether a watch is already is progress.
751 //
752 // See https://github.com/jackc/pgconn/issues/29
753 pgConn.contextWatcher.Unwatch()
754
755 pgConn.contextWatcher.Watch(ctx)
756 defer pgConn.contextWatcher.Unwatch()
757 }
758
759 // Ignore any errors sending Terminate message and waiting for server to close connection.
760 // This mimics the behavior of libpq PQfinish. It calls closePGconn which calls sendTerminateConn which purposefully
761 // ignores errors.
762 //
763 // See https://github.com/jackc/pgx/issues/637
764 pgConn.frontend.Send(&pgproto3.Terminate{})
765 pgConn.flushWithPotentialWriteReadDeadlock()
766
767 return pgConn.conn.Close()
768}
769
770// asyncClose marks the connection as closed and asynchronously sends a cancel query message and closes the underlying
771// connection.

Callers

nothing calls this directly

Calls 5

UnwatchMethod · 0.80
WatchMethod · 0.80
CloseMethod · 0.65
SendMethod · 0.45

Tested by

no test coverage detected