nbconn

package
v5.0.0-...-e466dda Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

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

Constants

This section is empty.

Variables

View Source
var ErrWouldBlock = new(wouldBlockError)
View Source
var NonBlockingDeadline = time.Date(1900, 1, 1, 0, 0, 0, 608536336, time.UTC)

NonBlockingDeadline is a magic value that when passed to Set[Read]Deadline places the connection in non-blocking read mode.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	net.Conn

	// Flush flushes any buffered writes.
	Flush() error

	// BufferReadUntilBlock reads and buffers any successfully 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 NewNetConn(conn net.Conn, fakeNonBlockingIO bool) *NetConn

func (*NetConn) BufferReadUntilBlock

func (c *NetConn) BufferReadUntilBlock() error

func (*NetConn) Close

func (c *NetConn) Close() (err error)

func (*NetConn) Flush

func (c *NetConn) Flush() error

func (*NetConn) LocalAddr

func (c *NetConn) LocalAddr() net.Addr

func (*NetConn) Read

func (c *NetConn) Read(b []byte) (n int, err error)

Read implements io.Reader.

func (*NetConn) RemoteAddr

func (c *NetConn) RemoteAddr() net.Addr

func (*NetConn) SetDeadline

func (c *NetConn) SetDeadline(t time.Time) error

SetDeadline is the equivalent of calling SetReadDealine(t) and SetWriteDeadline(t).

func (*NetConn) SetReadDeadline

func (c *NetConn) SetReadDeadline(t time.Time) error

SetReadDeadline sets the read deadline as t. If t == NonBlockingDeadline then future reads will be non-blocking.

func (*NetConn) SetWriteDeadline

func (c *NetConn) SetWriteDeadline(t time.Time) error

func (*NetConn) Write

func (c *NetConn) Write(b []byte) (n int, err error)

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

func TLSClient(conn *NetConn, config *tls.Config) (*TLSConn, error)

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 (tc *TLSConn) BufferReadUntilBlock() error

func (*TLSConn) Close

func (tc *TLSConn) Close() error

func (*TLSConn) Flush

func (tc *TLSConn) Flush() error

func (*TLSConn) LocalAddr

func (tc *TLSConn) LocalAddr() net.Addr

func (*TLSConn) Read

func (tc *TLSConn) Read(b []byte) (n int, err error)

func (*TLSConn) RemoteAddr

func (tc *TLSConn) RemoteAddr() net.Addr

func (*TLSConn) SetDeadline

func (tc *TLSConn) SetDeadline(t time.Time) error

func (*TLSConn) SetReadDeadline

func (tc *TLSConn) SetReadDeadline(t time.Time) error

func (*TLSConn) SetWriteDeadline

func (tc *TLSConn) SetWriteDeadline(t time.Time) error

func (*TLSConn) Write

func (tc *TLSConn) Write(b []byte) (n int, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL