buffer

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: MPL-2.0 Imports: 13 Imported by: 3

Documentation

Index

Constants

View Source
const DefaultBufferSize = 1 << 24 // 16777216 bytes

DefaultBufferSize represents the default buffer size whenever the buffer size is not set or a negative value is presented.

View Source
const MaxPreparedStatementArgs = math.MaxUint16

MaxPreparedStatementArgs is the maximum number of arguments a prepared statement can have when prepared via the Postgres wire protocol. This is not documented by Postgres, but is a consequence of the fact that a 16-bit integer in the wire format is used to indicate the number of values to bind during prepared statement execution.

Variables

View Source
var ErrInsufficientData = errors.New("insufficient data")

ErrInsufficientData is thrown when there is insufficient data available inside the given message to unmarshal into a given type.

View Source
var ErrMessageSizeExceeded = MessageSizeExceeded{Message: "maximum message size exceeded"}

ErrMessageSizeExceeded is thrown when the maximum message size is exceeded.

View Source
var ErrMissingNulTerminator = errors.New("NUL terminator not found")

ErrMissingNulTerminator is thrown when no NUL terminator is found when interperating a message property as a string.

Functions

func EncodeBoolean

func EncodeBoolean(value bool) string

EncodeBoolean returns a string value ("on"/"off") representing the given boolean value

func NewInsufficientData

func NewInsufficientData(length int) error

NewInsufficientData constructs a new error message wrapping the ErrInsufficientData type with additional metadata.

func NewMessageSizeExceeded

func NewMessageSizeExceeded(max, size int) error

NewMessageSizeExceeded constructs a new error message wrapping the ErrMaxMessageSizeExceeded type with additional metadata.

func NewMissingNulTerminator

func NewMissingNulTerminator() error

NewMissingNulTerminator constructs a new error message wrapping the ErrMissingNulTerminator type with additional metadata.

Types

type BufferedReader

type BufferedReader interface {
	io.Reader
	ReadString(delim byte) (string, error)
	ReadByte() (byte, error)
}

BufferedReader extended io.Reader with some convenience methods.

type MessageSizeExceeded

type MessageSizeExceeded struct {
	Message string
	Size    int
	Max     int
}

MessageSizeExceeded represents a error implementation which could be used to indicate that the message size limit has been exceeded. The message size and maximum message length could be included inside the struct.

func UnwrapMessageSizeExceeded

func UnwrapMessageSizeExceeded(err error) (result MessageSizeExceeded, _ bool)

UnwrapMessageSizeExceeded attempts to unwrap the given error as MessageSizeExceeded. A boolean is returned indicating whether the error contained a MessageSizeExceeded message.

func (MessageSizeExceeded) Error

func (err MessageSizeExceeded) Error() string

func (MessageSizeExceeded) Is

func (err MessageSizeExceeded) Is(target error) bool

type PrepareType

type PrepareType byte

PrepareType represents a subtype for prepare messages.

const (
	// PrepareStatement represents a prepared statement.
	PrepareStatement PrepareType = 'S'
	// PreparePortal represents a portal.
	PreparePortal PrepareType = 'P'
)

type Reader

type Reader struct {
	Buffer         BufferedReader
	Msg            []byte
	MaxMessageSize int
	// contains filtered or unexported fields
}

Reader provides a convenient way to read pgwire protocol messages

func NewReader

func NewReader(logger *slog.Logger, reader io.Reader, bufferSize int) *Reader

NewReader constructs a new Postgres wire buffer for the given io.Reader

func (*Reader) GetBytes

func (reader *Reader) GetBytes(n int) ([]byte, error)

GetBytes returns the buffer's contents as a []byte.

func (*Reader) GetPrepareType

func (reader *Reader) GetPrepareType() (PrepareType, error)

GetPrepareType returns the buffer's contents as a PrepareType.

func (*Reader) GetString

func (reader *Reader) GetString() (string, error)

GetString reads a null-terminated string.

func (*Reader) GetUint16

func (reader *Reader) GetUint16() (uint16, error)

GetUint16 returns the buffer's contents as a uint16.

func (*Reader) GetUint32

func (reader *Reader) GetUint32() (uint32, error)

GetUint32 returns the buffer's contents as a uint32.

func (*Reader) ReadMsgSize added in v0.12.0

func (reader *Reader) ReadMsgSize() (int, error)

ReadMsgSize reads the length of the next message from the provided reader.

func (*Reader) ReadType added in v0.12.0

func (reader *Reader) ReadType() (types.ClientMessage, error)

ReadType reads the client message type from the provided reader.

func (*Reader) ReadTypedMsg

func (reader *Reader) ReadTypedMsg() (types.ClientMessage, int, error)

ReadTypedMsg reads a message from the provided reader, returning its type code and body. It returns the message type, number of bytes read, and an error if there was one.

func (*Reader) ReadUntypedMsg

func (reader *Reader) ReadUntypedMsg() (int, error)

ReadUntypedMsg reads a length-prefixed message. It is only used directly during the authentication phase of the protocol; [ReadTypedMsg] is used at all other times. This returns the number of bytes read and an error, if there was one. The number of bytes returned can be non-zero even with an error (e.g. if data was read but didn't validate) so that we can more accurately measure network traffic.

If the error is related to consuming a buffer that is larger than the maxMessageSize, the remaining bytes will be read but discarded.

func (*Reader) Slurp

func (reader *Reader) Slurp(size int) error

Slurp reads the remaining

type ServerErrFieldType

type ServerErrFieldType byte

ServerErrFieldType represents the error fields.

const (
	ServerErrFieldSeverity       ServerErrFieldType = 'S'
	ServerErrFieldSQLState       ServerErrFieldType = 'C'
	ServerErrFieldMsgPrimary     ServerErrFieldType = 'M'
	ServerErrFieldDetail         ServerErrFieldType = 'D'
	ServerErrFieldHint           ServerErrFieldType = 'H'
	ServerErrFieldSrcFile        ServerErrFieldType = 'F'
	ServerErrFieldSrcLine        ServerErrFieldType = 'L'
	ServerErrFieldSrcFunction    ServerErrFieldType = 'R'
	ServerErrFieldConstraintName ServerErrFieldType = 'n'
)

http://www.postgresql.org/docs/current/static/protocol-error-fields.html

type Writer

type Writer struct {
	io.Writer
	// contains filtered or unexported fields
}

Writer provides a convenient way to write pgwire protocol messages

func NewWriter

func NewWriter(logger *slog.Logger, writer io.Writer) *Writer

NewWriter constructs a new Postgres buffered message writer for the given io.Writer

func (*Writer) AddByte

func (writer *Writer) AddByte(b byte)

AddByte writes the given byte to the writer frame. Bytes written to the frame could be read at any stage to interact with a Postgres client. Errors thrown while writing to the writer could be read by calling writer.Error()

func (*Writer) AddBytes

func (writer *Writer) AddBytes(b []byte) (size int)

AddBytes writes the given bytes to the writer frame. Bytes written to the frame could be read at any stage to interact with a Postgres client. Errors thrown while writing to the writer could be read by calling writer.Error()

func (*Writer) AddInt16

func (writer *Writer) AddInt16(i int16) (size int)

AddInt16 writes the given unsigned int16 to the writer frame. Bytes written to the frame could be read at any stage to interact with a Postgres client. Errors thrown while writing to the writer could be read by calling writer.Error()

func (*Writer) AddInt32

func (writer *Writer) AddInt32(i int32) (size int)

AddInt32 writes the given unsigned int32 to the writer frame. Bytes written to the frame could be read at any stage to interact with a Postgres client. Errors thrown while writing to the writer could be read by calling writer.Error()

func (*Writer) AddNullTerminate

func (writer *Writer) AddNullTerminate()

AddNullTerminate writes a null terminate symbol to the end of the given data frame

func (*Writer) AddString

func (writer *Writer) AddString(s string) (size int)

AddString writes the given string to the writer frame. Bytes written to the frame could be read at any stage to interact with a Postgres client. Errors thrown while writing to the writer could be read by calling writer.Error()

func (*Writer) Bytes

func (writer *Writer) Bytes() []byte

Bytes returns the written bytes to the active data frame

func (*Writer) End

func (writer *Writer) End() error

End writes the prepared message to the given writer and resets the buffer. The to be expected message length is appended after the message status byte.

func (*Writer) Error

func (writer *Writer) Error() error

func (*Writer) Reset

func (writer *Writer) Reset()

Reset resets the data frame to be empty

func (*Writer) Start

func (writer *Writer) Start(t types.ServerMessage)

Start resets the buffer writer and starts a new message with the given message type. The message type (byte) and reserved message length bytes (int32) are written to the underlaying bytes buffer.

Jump to

Keyboard shortcuts

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