wire

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2026 License: MIT Imports: 6 Imported by: 0

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

View Source
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

View Source
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) Byte

func (b *Builder) Byte(v byte)

func (*Builder) Bytes

func (b *Builder) Bytes(p []byte)

func (*Builder) CString

func (b *Builder) CString(s string)

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().

func (*Builder) Int16

func (b *Builder) Int16(v int16)

func (*Builder) Int32

func (b *Builder) Int32(v int32)

func (*Builder) Send

func (b *Builder) Send() error

Send patches the length prefix and flushes the entire write buffer to the connection. Equivalent to Finish + c.Flush. Safe to call as the terminal action after multiple Begin → Finish pairs; it will flush everything accumulated so far.

func (*Builder) String

func (b *Builder) String(s string)

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

func New

func New(nc net.Conn) *Conn

New wraps nc with the default reader buffer size.

func NewSized

func NewSized(nc net.Conn, bufSize int) *Conn

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

func (c *Conn) Begin(t byte) *Builder

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

func (c *Conn) BeginNoType() *Builder

BeginNoType is for the StartupMessage / SSLRequest / CancelRequest messages, which omit the type byte.

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) Flush

func (c *Conn) Flush() error

Flush writes any accumulated (already Finished) messages and resets the write buffer. Idempotent on an empty buffer.

func (*Conn) Net

func (c *Conn) Net() net.Conn

func (*Conn) ReadByte

func (c *Conn) ReadByte() (byte, error)

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

func (c *Conn) ReadMessage() (byte, []byte, error)

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.

func (*Conn) SwapNet

func (c *Conn) SwapNet(nc net.Conn)

SwapNet replaces the underlying connection (used after the TLS upgrade). The scratch buffers are kept.

func (*Conn) WriteRaw

func (c *Conn) WriteRaw(p []byte) error

WriteRaw writes the given bytes verbatim. Used by the startup message which has its own framing rules.

Jump to

Keyboard shortcuts

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