package nbconn
import "github.com/jackc/pgx/v5/internal/nbconn"
Package nbconn implements a non-blocking net.Conn wrapper.
It is designed to solve three problems.
The first is resolving the deadlock that can occur when both sides of a connection are blocked writing because all buffers between are full. See https://github.com/jackc/pgconn/issues/27 for discussion.
The second is the inability to use a write deadline with a TLS.Conn without killing the connection.
The third is to efficiently check if a connection has been closed via a non-blocking read.
Index ¶
- Variables
- type Conn
- type NetConn
- func NewNetConn(conn net.Conn, fakeNonBlockingIO bool) *NetConn
- func (c *NetConn) BufferReadUntilBlock() error
- func (c *NetConn) Close() (err error)
- func (c *NetConn) Flush() error
- func (c *NetConn) LocalAddr() net.Addr
- func (c *NetConn) Read(b []byte) (n int, err error)
- func (c *NetConn) RemoteAddr() net.Addr
- func (c *NetConn) SetDeadline(t time.Time) error
- func (c *NetConn) SetReadDeadline(t time.Time) error
- func (c *NetConn) SetWriteDeadline(t time.Time) error
- func (c *NetConn) Write(b []byte) (n int, err error)
- type TLSConn
- func TLSClient(conn *NetConn, config *tls.Config) (*TLSConn, error)
- func (tc *TLSConn) BufferReadUntilBlock() error
- func (tc *TLSConn) Close() error
- func (tc *TLSConn) Flush() error
- func (tc *TLSConn) LocalAddr() net.Addr
- func (tc *TLSConn) Read(b []byte) (n int, err error)
- func (tc *TLSConn) RemoteAddr() net.Addr
- func (tc *TLSConn) SetDeadline(t time.Time) error
- func (tc *TLSConn) SetReadDeadline(t time.Time) error
- func (tc *TLSConn) SetWriteDeadline(t time.Time) error
- func (tc *TLSConn) Write(b []byte) (n int, err error)
Variables ¶
var ErrWouldBlock = new(wouldBlockError)
NonBlockingDeadline is a magic value that when passed to Set[Read]Deadline places the connection in non-blocking read mode.
Types ¶
type Conn ¶
type Conn interface { net.Conn // Flush flushes any buffered writes. Flush() error // BufferReadUntilBlock reads and buffers any sucessfully read bytes until the read would block. BufferReadUntilBlock() error }
Conn is a net.Conn where Write never blocks and always succeeds. Flush or Read must be called to actually write to the underlying connection.
type NetConn ¶
type NetConn struct {
// contains filtered or unexported fields
}
NetConn is a non-blocking net.Conn wrapper. It implements net.Conn.
func NewNetConn ¶
func (*NetConn) BufferReadUntilBlock ¶
func (*NetConn) Close ¶
func (*NetConn) Flush ¶
func (*NetConn) LocalAddr ¶
func (*NetConn) Read ¶
Read implements io.Reader.
func (*NetConn) RemoteAddr ¶
func (*NetConn) SetDeadline ¶
SetDeadline is the equivalent of calling SetReadDealine(t) and SetWriteDeadline(t).
func (*NetConn) SetReadDeadline ¶
SetReadDeadline sets the read deadline as t. If t == NonBlockingDeadline then future reads will be non-blocking.
func (*NetConn) SetWriteDeadline ¶
func (*NetConn) Write ¶
Write implements io.Writer. It never blocks due to buffering all writes. It will only return an error if the Conn is closed. Call Flush to actually write to the underlying connection.
type TLSConn ¶
type TLSConn struct {
// contains filtered or unexported fields
}
TLSConn is a TLS wrapper around a *Conn. It works around a temporary write error (such as a timeout) being fatal to a tls.Conn.
func TLSClient ¶
TLSClient establishes a TLS connection as a client over conn using config.
To avoid the first Read on the returned *TLSConn also triggering a Write due to the TLS handshake and thereby potentially causing a read and write deadlines to behave unexpectedly, Handshake is called explicitly before the *TLSConn is returned.
func (*TLSConn) BufferReadUntilBlock ¶
func (*TLSConn) Close ¶
func (*TLSConn) Flush ¶
func (*TLSConn) LocalAddr ¶
func (*TLSConn) Read ¶
func (*TLSConn) RemoteAddr ¶
func (*TLSConn) SetDeadline ¶
func (*TLSConn) SetReadDeadline ¶
func (*TLSConn) SetWriteDeadline ¶
func (*TLSConn) Write ¶
Source Files ¶
bufferqueue.go nbconn.go nbconn_real_non_block.go
- Version
- v5.0.2
- Published
- Oct 8, 2022
- Platform
- linux/amd64
- Imports
- 10 packages
- Last checked
- 3 hours ago –
Tools for package owners.