Documentation
¶
Overview ¶
Package wire is the framed reader/writer over a net.Conn. It owns one scratch read buffer per connection that is reused across messages; the slice returned by ReadMessage is valid only until the next ReadMessage call. This is the central allocation-reduction trick of the driver.
Index ¶
- Constants
- Variables
- type Builder
- type Conn
- func (c *Conn) Begin(t byte) *Builder
- func (c *Conn) BeginNoType() *Builder
- func (c *Conn) Close() error
- func (c *Conn) Flush() error
- func (c *Conn) Net() net.Conn
- func (c *Conn) ReadByte() (byte, error)
- func (c *Conn) ReadMessage() (byte, []byte, error)
- func (c *Conn) SwapNet(nc net.Conn)
- func (c *Conn) WriteRaw(p []byte) error
Constants ¶
const DefaultReaderBufSize = 128 * 1024
Conn wraps a net.Conn with a single growable read buffer and a small write buffer for building outgoing messages.
Reads go through a bufio.Reader sized at readerBufSize. With narrow rows (a single int4 column) each DataRow is ~11–13 bytes over the wire; raw net.Conn reads would translate to ~2 syscalls per row. The bufio layer amortizes that to one syscall per ~4 KB of wire traffic, which is the difference between ~20 MB/s and ~100 MB/s of JSON output on small-row queries. The read buffer is still the growable `readBuf` below — that's the slice we alias into and return from ReadMessage; the bufio just feeds it from userspace, not from the kernel each time. DefaultReaderBufSize is the default bufio.Reader size used when the caller does not override it via Config.WireReadBufferSize. 128 KiB covers essentially every DataRow shape while fitting comfortably in L2 on current hardware.
Variables ¶
var ErrUnexpected = errors.New("wire: unexpected message")
ErrUnexpected is a sentinel for control-flow asserts.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is the in-flight outgoing message. All Append* methods write to the connection's reusable buffer.
func (*Builder) Finish ¶
func (b *Builder) Finish()
Finish patches the length prefix of this message in place but does NOT write. Use when pipelining several messages before a single c.Flush().
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
func NewSized ¶
NewSized wraps nc with a caller-specified bufio size. Sizes smaller than 4 KiB are clamped to 4 KiB by the Config layer.
func (*Conn) Begin ¶
Begin starts building an outgoing message of the given type. It appends to the connection's write buffer without truncating prior content, so multiple Begin → Finish calls can accumulate into a single TCP write (pipelining). The final call should be Send (which flushes) or Finish followed by an explicit c.Flush() when the caller wants to send several messages at once.
func (*Conn) BeginNoType ¶
BeginNoType is for the StartupMessage / SSLRequest / CancelRequest messages, which omit the type byte.
func (*Conn) Flush ¶
Flush writes any accumulated (already Finished) messages and resets the write buffer. Idempotent on an empty buffer.
func (*Conn) ReadByte ¶
ReadByte reads exactly one byte. Used for the SSLRequest reply, which returns 'S' or 'N' without any framing. Reads bypass the bufio layer since this happens before any framed traffic.
func (*Conn) ReadMessage ¶
ReadMessage reads one v3 message and returns its type byte and the body (without the 4-byte length prefix). The returned slice aliases an internal buffer and is invalidated by the next ReadMessage call.