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
- func AppendCommand(b []byte, args ...string) []byte
- func IsProtocolError(err error) bool
- func ParseCommand(dst [][]byte, buf []byte) (args [][]byte, n int, inline bool, err error)
- type Encoder
- type Kind
- type Parser
- type ProtocolError
- type Recorder
- func (r *Recorder) Array(n int)
- func (r *Recorder) Bulk(s string)
- func (r *Recorder) BulkBytes(b []byte)
- func (r *Recorder) Error(msg string)
- func (r *Recorder) Int(n int64)
- func (r *Recorder) Null()
- func (r *Recorder) NullArray()
- func (r *Recorder) Reset()
- func (r *Recorder) Result() Value
- func (r *Recorder) Results() []Value
- func (r *Recorder) Simple(s string)
- type Value
- type Writer
Constants ¶
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 ¶
AppendCommand encodes args as a RESP array of bulk strings.
func IsProtocolError ¶
IsProtocolError reports whether err is a *ProtocolError.
func ParseCommand ¶
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.
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 ¶
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.
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.
type 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 (Value) Interface ¶
Interface converts v into plain Go values for JSON encoding: strings, int64, nil and []any. Errors become map{"error": msg}.