Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

proto

v9.24.1
github.com/redis/go-redis/v9/internal/proto

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

134 matches

Top ↑

Constants

RespStatus (15 declarations)

const (
	RespStatus    = '+' // +<string>\r\n
	RespError     = '-' // -<string>\r\n
	RespString    = '$' // $<length>\r\n<bytes>\r\n
	RespInt       = ':' // :<number>\r\n
	RespNil       = '_' // _\r\n
	RespFloat     = ',' // ,<floating-point-number>\r\n (golang float)
	RespBool      = '#' // true: #t\r\n false: #f\r\n
	RespBlobError = '!' // !<length>\r\n<bytes>\r\n
	RespVerbatim  = '=' // =<length>\r\nFORMAT:<bytes>\r\n
	RespBigInt    = '(' // (<big number>\r\n
	RespArray     = '*' // *<len>\r\n... (same as resp2)
	RespMap       = '%' // %<len>\r\n(key)\r\n(value)\r\n... (golang map)
	RespSet       = '~' // ~<len>\r\n... (same as Array)
	RespAttr      = '|' // |<len>\r\n(key)\r\n(value)\r\n... + command reply
	RespPush      = '>' // ><len>\r\n... (same as Array)
)

redis resp protocol data type.

DefaultBufferSize

const DefaultBufferSize = 32 * 1024

DefaultBufferSize is the default size for read/write buffers (32 KiB).

MinRESP3ReadBufferSize

const MinRESP3ReadBufferSize = 128

MinRESP3ReadBufferSize is the minimum buffer size used when RESP3 push notifications must be inspected without consuming them.

Nil

const Nil = RedisError("redis: nil") // nolint:errname

Variables

ErrInvalidVerbatimString

var ErrInvalidVerbatimString = errors.New("redis: invalid verbatim string")

ErrInvalidVerbatimString reports a malformed, fully consumed RESP3 verbatim string. Parsing may resume at the next frame.

ErrPushNotificationNameNotString

var ErrPushNotificationNameNotString = errors.New("redis: push notification name is not a string")

ErrPushNotificationNameNotString is returned when the next push frame has a well-formed header but its first element is a RESP type other than a string, so there is no name to peek. Like ErrPushNotificationNameTooLong the header gives ReadReply a frame to consume, so callers should consume it with ReadReply. Only the type byte of the first element is checked: a body that does not parse still stops ReadReply partway, as it does for a push with a valid name. Any other error means there is nothing to consume: either the frame has not fully arrived (I/O error) or its header does not parse.

ErrPushNotificationNameTooLong

var ErrPushNotificationNameTooLong = errors.New("redis: push notification name exceeds peek window")

ErrPushNotificationNameTooLong is returned when the push header does not fit in the bounded peek window. Callers should consume the frame with ReadReply.

Functions

IsAuthError

func IsAuthError(err error) bool

IsAuthError checks if an error is an AuthError, even if wrapped.

IsClusterDownError

func IsClusterDownError(err error) bool

IsClusterDownError checks if an error is a ClusterDownError, even if wrapped.

IsExecAbortError

func IsExecAbortError(err error) bool

IsExecAbortError checks if an error is an ExecAbortError, even if wrapped.

IsLoadingError

func IsLoadingError(err error) bool

IsLoadingError checks if an error is a LoadingError, even if wrapped.

IsMasterDownError

func IsMasterDownError(err error) bool

IsMasterDownError checks if an error is a MasterDownError, even if wrapped.

IsMaxClientsError

func IsMaxClientsError(err error) bool

IsMaxClientsError checks if an error is a MaxClientsError, even if wrapped.

IsNilReply

func IsNilReply(line []byte) bool

IsNilReply detects redis.Nil of RESP2.

IsNoReplicasError

func IsNoReplicasError(err error) bool

IsNoReplicasError checks if an error is a NoReplicasError, even if wrapped.

IsOOMError

func IsOOMError(err error) bool

IsOOMError checks if an error is an OOMError, even if wrapped.

IsPermissionError

func IsPermissionError(err error) bool

IsPermissionError checks if an error is a PermissionError, even if wrapped.

IsReadOnlyError

func IsReadOnlyError(err error) bool

IsReadOnlyError checks if an error is a ReadOnlyError, even if wrapped.

IsTryAgainError

func IsTryAgainError(err error) bool

IsTryAgainError checks if an error is a TryAgainError, even if wrapped.

ParseErrorReply

func ParseErrorReply(line []byte) error

Scan

func Scan(b []byte, v any) error

Scan parses bytes `b` to `v` with appropriate type.

ScanSlice

func ScanSlice(data []string, slice any) error

Types

AskError

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

AskError is returned when a key is being migrated and the client should ask another node.

IsAskError

func IsAskError(err error) (*AskError, bool)

IsAskError checks if an error is an AskError, even if wrapped. Returns the error and a boolean indicating if it's an AskError.

NewAskError

func NewAskError(msg string, addr string) *AskError

NewAskError creates a new AskError with the given message and address.

AskError.Addr

func (e *AskError) Addr() string

Addr returns the address of the node to ask.

AskError.Error

func (e *AskError) Error() string

AskError.RedisError

func (e *AskError) RedisError()

AuthError

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

AuthError is returned when authentication fails.

NewAuthError

func NewAuthError(msg string) *AuthError

NewAuthError creates a new AuthError with the given message.

AuthError.Error

func (e *AuthError) Error() string

AuthError.RedisError

func (e *AuthError) RedisError()

ClusterDownError

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

ClusterDownError is returned when the cluster is down.

NewClusterDownError

func NewClusterDownError(msg string) *ClusterDownError

NewClusterDownError creates a new ClusterDownError with the given message.

ClusterDownError.Error

func (e *ClusterDownError) Error() string

ClusterDownError.RedisError

func (e *ClusterDownError) RedisError()

ExecAbortError

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

ExecAbortError is returned when a transaction is aborted.

NewExecAbortError

func NewExecAbortError(msg string) *ExecAbortError

NewExecAbortError creates a new ExecAbortError with the given message.

ExecAbortError.Error

func (e *ExecAbortError) Error() string

ExecAbortError.RedisError

func (e *ExecAbortError) RedisError()

LoadingError

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

LoadingError is returned when Redis is loading the dataset in memory.

NewLoadingError

func NewLoadingError(msg string) *LoadingError

NewLoadingError creates a new LoadingError with the given message.

LoadingError.Error

func (e *LoadingError) Error() string

LoadingError.RedisError

func (e *LoadingError) RedisError()

MasterDownError

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

MasterDownError is returned when the master is down.

NewMasterDownError

func NewMasterDownError(msg string) *MasterDownError

NewMasterDownError creates a new MasterDownError with the given message.

MasterDownError.Error

func (e *MasterDownError) Error() string

MasterDownError.RedisError

func (e *MasterDownError) RedisError()

MaxClientsError

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

MaxClientsError is returned when the maximum number of clients has been reached.

NewMaxClientsError

func NewMaxClientsError(msg string) *MaxClientsError

NewMaxClientsError creates a new MaxClientsError with the given message.

MaxClientsError.Error

func (e *MaxClientsError) Error() string

MaxClientsError.RedisError

func (e *MaxClientsError) RedisError()

MovedError

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

MovedError is returned when a key has been moved to a different node in a cluster.

IsMovedError

func IsMovedError(err error) (*MovedError, bool)

IsMovedError checks if an error is a MovedError, even if wrapped. Returns the error and a boolean indicating if it's a MovedError.

NewMovedError

func NewMovedError(msg string, addr string) *MovedError

NewMovedError creates a new MovedError with the given message and address.

MovedError.Addr

func (e *MovedError) Addr() string

Addr returns the address of the node where the key has been moved.

MovedError.Error

func (e *MovedError) Error() string

MovedError.RedisError

func (e *MovedError) RedisError()

NoReplicasError

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

NoReplicasError is returned when not enough replicas acknowledge a write. This error occurs when using WAIT/WAITAOF commands or CLUSTER SETSLOT with synchronous replication, and the required number of replicas cannot confirm the write within the timeout period.

NewNoReplicasError

func NewNoReplicasError(msg string) *NoReplicasError

NewNoReplicasError creates a new NoReplicasError with the given message.

NoReplicasError.Error

func (e *NoReplicasError) Error() string

NoReplicasError.RedisError

func (e *NoReplicasError) RedisError()

OOMError

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

OOMError is returned when Redis is out of memory.

NewOOMError

func NewOOMError(msg string) *OOMError

NewOOMError creates a new OOMError with the given message.

OOMError.Error

func (e *OOMError) Error() string

OOMError.RedisError

func (e *OOMError) RedisError()

PermissionError

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

PermissionError is returned when a user lacks required permissions.

NewPermissionError

func NewPermissionError(msg string) *PermissionError

NewPermissionError creates a new PermissionError with the given message.

PermissionError.Error

func (e *PermissionError) Error() string

PermissionError.RedisError

func (e *PermissionError) RedisError()

ReadOnlyError

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

ReadOnlyError is returned when trying to write to a read-only replica.

NewReadOnlyError

func NewReadOnlyError(msg string) *ReadOnlyError

NewReadOnlyError creates a new ReadOnlyError with the given message.

ReadOnlyError.Error

func (e *ReadOnlyError) Error() string

ReadOnlyError.RedisError

func (e *ReadOnlyError) RedisError()

Reader

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

NewReader

func NewReader(rd io.Reader) *Reader

NewReaderSize

func NewReaderSize(rd io.Reader, size int) *Reader

Reader.Buffered

func (r *Reader) Buffered() int

Reader.Discard

func (r *Reader) Discard(line []byte) (err error)

Discard the data represented by line.

Reader.DiscardCachedReadError

func (r *Reader) DiscardCachedReadError()

DiscardCachedReadError drops a read error that bufio cached from a previous operation, leaving the parser state alone. A transport can return a complete reply and a timeout together: the reply parses, but the wrapped timeout stays cached and would be reported for the next command on the connection.

Bytes already buffered are kept: the cached error can sit behind frames the previous command left in the buffer (a push, or the start of the next reply). bufio has no API to clear its cached error without a Reset, so the buffered bytes are copied out and replayed ahead of the underlying reader. The connection only calls this after a read returned bytes together with an error, and once per such read: on the common path the buffer is left untouched.

Reader.DiscardNext

func (r *Reader) DiscardNext() error

DiscardNext read and discard the data represented by the next line.

Reader.HasBufferedFrame

func (r *Reader) HasBufferedFrame() bool

HasBufferedFrame reports whether the buffer already holds the whole next frame, with any attributes in front of it, so reading it needs no socket read. It does not read from the socket and consumes nothing.

Reader.HasBufferedReply

func (r *Reader) HasBufferedReply() bool

HasBufferedReply reports whether the buffer already holds one complete reply, with any push frames and attributes in front of it, so reading that reply needs no socket read. It does not read from the socket and consumes nothing. A frame it does not know, a malformed one, or a streamed aggregate reports false; the caller then reads it the normal way, which reports any error.

Reader.Peek

func (r *Reader) Peek(n int) ([]byte, error)

Reader.PeekPushNotificationName

func (r *Reader) PeekPushNotificationName() (string, error)

PeekPushNotificationName returns the notification name of the next RESP3 push frame without consuming it. The caller is expected to have already verified that the next reply is a push notification (e.g. via PeekReplyType returning RespPush).

To identify the name the method may block reading more bytes from the underlying connection, but only ever waits for one byte beyond the valid frame prefix it has already seen. That byte is guaranteed to arrive: an incomplete prefix means the server is still committed to sending the rest of the frame. Demanding any fixed amount instead can deadlock — a complete frame such as a subscribe confirmation for a short channel name can be smaller than the fixed window, and once it is buffered the server has nothing more to send (issue #3935). Blocking for in-flight bytes is preferred to a truncated peek, which would silently misidentify the notification and cause the caller's ReadReply to consume (and drop) the frame; see issue #3839.

Reader.PeekReplyType

func (r *Reader) PeekReplyType() (byte, error)

PeekReplyType returns the data type of the next response without advancing the Reader, and discard the attribute type.

Reader.ReadArrayLen

func (r *Reader) ReadArrayLen() (int, error)

ReadArrayLen Read and return the length of the array.

Reader.ReadAttributeLen

func (r *Reader) ReadAttributeLen() (int, error)

ReadAttributeLen consumes a RESP3 attribute header and returns its pair count, leaving the pairs and wrapped reply unread.

Reader.ReadBool

func (r *Reader) ReadBool() (bool, error)

Reader.ReadFixedArrayLen

func (r *Reader) ReadFixedArrayLen(fixedLen int) error

ReadFixedArrayLen read fixed array length.

Reader.ReadFixedMapLen

func (r *Reader) ReadFixedMapLen(fixedLen int) error

ReadFixedMapLen reads fixed map length.

Reader.ReadFloat

func (r *Reader) ReadFloat() (float64, error)

Reader.ReadInt

func (r *Reader) ReadInt() (int64, error)

Reader.ReadLine

func (r *Reader) ReadLine() ([]byte, error)

ReadLine Return a valid reply, it will check the protocol or redis error, and discard the attribute type.

Reader.ReadMapLen

func (r *Reader) ReadMapLen() (int, error)

ReadMapLen reads the length of the map type. If responding to the array type (RespArray/RespSet/RespPush), it must be a multiple of 2 and return n/2. Other types will return an error.

Reader.ReadRawReply

func (r *Reader) ReadRawReply() ([]byte, error)

ReadRawReply reads the next RESP reply and returns it as raw bytes without parsing.

Reader.ReadRawReplyWriteTo

func (r *Reader) ReadRawReplyWriteTo(w io.Writer) (int64, error)

ReadRawReplyWriteTo streams the next RESP reply directly to w without intermediate allocations. Returns the number of bytes written and any error encountered.

Reader.ReadReply

func (r *Reader) ReadReply() (interface{}, error)

Reader.ReadSlice

func (r *Reader) ReadSlice() ([]interface{}, error)

Reader.ReadString

func (r *Reader) ReadString() (string, error)

Reader.ReadStringInto

func (r *Reader) ReadStringInto(buf []byte) (int, error)

ReadStringInto reads a string-typed reply directly into buf, avoiding the per-call allocation that ReadString incurs. It returns the number of bytes written to buf.

Supported reply types:

  • $<n>\r\n<payload>\r\n bulk string (the GET path; payload is read straight into buf via bufio.Reader — for payloads larger than the bufio buffer this is effectively zero-copy from the socket)
  • +<status>\r\n simple string, copied from the header line
  • :<int>\r\n integer, copied as its ASCII representation
  • ,<float>\r\n float, copied as its ASCII representation

Errors, nil, push notifications, and RESP3 attributes are intercepted by ReadLine and surfaced through err. RESP3 verbatim strings (=<n>\r\n<txt:payload>\r\n) are intentionally not handled — they are never returned by GET-family commands, and including them re-introduces a hazard class where the response-type byte read from a stale `line[0]` after a bufio refill can be misinterpreted as the verbatim format tag.

If the bulk payload does not fit in buf, an error is returned and the payload plus the trailing CRLF are drained from the reader so the connection stays aligned for the next reply. For simple-string / integer / float responses the payload lives in the (already-consumed) header line, so no drain is needed.

Reader.ReadUint

func (r *Reader) ReadUint() (uint64, error)

Reader.Reset

func (r *Reader) Reset(rd io.Reader)

Reader.Size

func (r *Reader) Size() int

Size returns the size of the underlying read buffer.

RedisError

type RedisError string

RedisError.Error

func (e RedisError) Error() string

RedisError.RedisError

func (RedisError) RedisError()

TryAgainError

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

TryAgainError is returned when a command cannot be processed and should be retried.

NewTryAgainError

func NewTryAgainError(msg string) *TryAgainError

NewTryAgainError creates a new TryAgainError with the given message.

TryAgainError.Error

func (e *TryAgainError) Error() string

TryAgainError.RedisError

func (e *TryAgainError) RedisError()

Writer

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

NewWriter

func NewWriter(wr writer) *Writer

Writer.Reset

func (w *Writer) Reset(wr writer)

Reset points an existing Writer at a new destination, keeping lenBuf and numBuf. It exists so callers on a hot path can pool Writers instead of allocating one (plus its two scratch buffers) per use.

Pass nil when returning a Writer to a pool so it does not retain the old destination.

Writer.WriteArg

func (w *Writer) WriteArg(v interface{}) error

Writer.WriteArgs

func (w *Writer) WriteArgs(args []interface{}) error

Dependency graph