Help us shape the future of Go! Take the 2026 Go Developer Survey

resp

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package resp implements the Redis Serialization Protocol (RESP2): an incremental, zero-copy command parser that also accepts inline commands (what you type in telnet), and reply writers.

Index

Constants

View Source
const (
	// MaxBulkLen is the largest accepted bulk string (same as Redis).
	MaxBulkLen = 512 << 20
	// MaxArgs bounds the number of arguments in one command.
	MaxArgs = 1 << 20
	// MaxInlineLen bounds inline command lines.
	MaxInlineLen = 64 << 10
)

Variables

This section is empty.

Functions

func AppendCommand

func AppendCommand(b []byte, args ...string) []byte

AppendCommand encodes args as a RESP array of bulk strings.

func IsProtocolError

func IsProtocolError(err error) bool

IsProtocolError reports whether err is a *ProtocolError.

func ParseCommand

func ParseCommand(dst [][]byte, buf []byte) (args [][]byte, n int, inline bool, err error)

ParseCommand parses one command from the start of buf. The returned arguments alias buf (they're valid until buf is modified) and are appended to dst. n is the number of bytes consumed; n == 0 with a nil error means more data is needed. inline reports whether the command used the inline format. Empty lines and empty arrays are consumed and return no args.

ParseCommand is stateless: it re-parses a partial command from the start on every call. Network servers should use a Parser instead.

Types

type Encoder

type Encoder struct{ Buf []byte }

Encoder appends RESP2 encoded replies to Buf.

func (*Encoder) Array

func (e *Encoder) Array(n int)

func (*Encoder) Bulk

func (e *Encoder) Bulk(s string)

func (*Encoder) BulkBytes

func (e *Encoder) BulkBytes(b []byte)

func (*Encoder) Error

func (e *Encoder) Error(msg string)

Error writes an error reply. Messages without an upper-case error code prefix get "ERR " prepended, and line breaks are replaced so the reply stays a single line.

func (*Encoder) Int

func (e *Encoder) Int(n int64)

func (*Encoder) Null

func (e *Encoder) Null()

func (*Encoder) NullArray

func (e *Encoder) NullArray()

func (*Encoder) Simple

func (e *Encoder) Simple(s string)

type Kind

type Kind uint8

Kind identifies the type of a reply Value.

const (
	KindSimple Kind = iota
	KindError
	KindInt
	KindBulk
	KindNull
	KindArray
	KindNullArray
)

type Parser

type Parser struct {
	// MaxArgs and MaxBulk limit the next command (0 = package defaults).
	// Servers lower them for unauthenticated clients, like Redis does.
	MaxArgs int
	MaxBulk int
	// contains filtered or unexported fields
}

Parser parses commands incrementally. While a multibulk command is only partially received it remembers the arguments already parsed and how many bytes it needs next, so a large command arriving in many small reads is parsed in linear time.

Between calls the caller must keep the unconsumed bytes in place: buf has to start at the same command and only grow by appending.

func (*Parser) Need

func (p *Parser) Need() int

Need returns how many bytes (counted from the start of the pending command) must be buffered before Parse can make progress, or 0 if unknown.

func (*Parser) Parse

func (p *Parser) Parse(dst [][]byte, buf []byte) (args [][]byte, n int, inline bool, err error)

Parse works like ParseCommand but resumes partially parsed commands.

func (*Parser) Reset

func (p *Parser) Reset()

Reset discards any partial command.

type ProtocolError

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

ProtocolError is returned for malformed input. The connection should be closed after reporting it.

func (*ProtocolError) Error

func (e *ProtocolError) Error() string

type Recorder

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

Recorder is a Writer that builds Values. After a complete reply has been written, Result returns it.

func (*Recorder) Array

func (r *Recorder) Array(n int)

func (*Recorder) Bulk

func (r *Recorder) Bulk(s string)

func (*Recorder) BulkBytes

func (r *Recorder) BulkBytes(b []byte)

func (*Recorder) Error

func (r *Recorder) Error(msg string)

func (*Recorder) Int

func (r *Recorder) Int(n int64)

func (*Recorder) Null

func (r *Recorder) Null()

func (*Recorder) NullArray

func (r *Recorder) NullArray()

func (*Recorder) Reset

func (r *Recorder) Reset()

Reset clears recorded replies.

func (*Recorder) Result

func (r *Recorder) Result() Value

Result returns the first complete reply written.

func (*Recorder) Results

func (r *Recorder) Results() []Value

Results returns all complete replies written so far.

func (*Recorder) Simple

func (r *Recorder) Simple(s string)

type Value

type Value struct {
	Kind  Kind
	Str   string // Simple, Error, Bulk
	Int   int64
	Array []Value
}

Value is a decoded reply. It's used where a reply must be inspected rather than streamed: Lua scripts, the REST /command endpoint, the legacy text protocol and the client.

func ReadValue

func ReadValue(r *bufio.Reader) (Value, error)

ReadValue reads one RESP2 reply from r (used by the client and tests).

func (Value) Interface

func (v Value) Interface() any

Interface converts v into plain Go values for JSON encoding: strings, int64, nil and []any. Errors become map{"error": msg}.

func (Value) IsNil

func (v Value) IsNil() bool

IsNil reports whether v is a null bulk string or null array.

func (Value) String

func (v Value) String() string

String renders v the way redis-cli does.

func (Value) Write

func (v Value) Write(w Writer)

Write replays v into w.

type Writer

type Writer interface {
	Simple(s string)
	Error(msg string)
	Int(n int64)
	Bulk(s string)
	BulkBytes(b []byte)
	Null()
	Array(n int)
	NullArray()
}

Writer receives a command reply. Arrays are written as Array(n) followed by exactly n elements (which may themselves be arrays).

Jump to

Keyboard shortcuts

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