redis

package
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

redis command & resp 适配,设计目标网络读取和解析同步进行。

Documentation

Overview

Package redcon implements a Redis compatible server framework

Index

Constants

View Source
const (
	MaxBulkBytesLen = 1024 * 1024 * 512
	MaxArrayLen     = 1024 * 1024
)

Variables

View Source
var (
	ErrBadCRLFEnd = errors.New("bad CRLF end")

	ErrBadArrayLen        = errors.New("bad array len")
	ErrBadArrayLenTooLong = errors.New("bad array len, too long")

	ErrBadBulkBytesLen        = errors.New("bad bulk bytes len")
	ErrBadBulkBytesLenTooLong = errors.New("bad bulk bytes len, too long")

	ErrBadMultiBulkLen     = errors.New("bad multi-bulk len")
	ErrBadMultiBulkContent = errors.New("bad multi-bulk content, should be bulkbytes")
)
View Source
var ErrFailedDecoder = errors.New("use of failed decoder")
View Source
var ErrFailedEncoder = errors.New("use of failed encoder")
View Source
var (
	ErrInvalid = errors.New("invalid redis packet")
)

Functions

func AppendArray

func AppendArray(b []byte, n int) []byte

AppendArray appends a Redis protocol array to the input bytes.

func AppendBulk

func AppendBulk(b []byte, bulk []byte) []byte

AppendBulk appends a Redis protocol bulk byte slice to the input bytes.

func AppendBulkString

func AppendBulkString(b []byte, bulk string) []byte

AppendBulkString appends a Redis protocol bulk string to the input bytes.

func AppendError

func AppendError(b []byte, s string) []byte

AppendError appends a Redis protocol error to the input bytes.

func AppendInt

func AppendInt(b []byte, n int64) []byte

AppendInt appends a Redis protocol int64 to the input bytes.

func AppendNull

func AppendNull(b []byte) []byte

AppendNull appends a Redis protocol null to the input bytes.

func AppendOK

func AppendOK(b []byte) []byte

AppendOK appends a Redis protocol OK to the input bytes.

func AppendString

func AppendString(b []byte, s string) []byte

AppendString appends a Redis protocol string to the input bytes.

func AppendTile38

func AppendTile38(b []byte, data []byte) []byte

AppendTile38 appends a Tile38 message to the input bytes.

func AppendUint

func AppendUint(b []byte, n uint64) []byte

AppendUint appends a Redis protocol uint64 to the input bytes.

func Btoi64

func Btoi64(b []byte) (int64, error)

func Encode

func Encode(w io.Writer, r *Resp) error

func EncodeToBytes

func EncodeToBytes(r *Resp) ([]byte, error)

func ListenAndServe

func ListenAndServe(addr string,
	handler func(conn RedisConn, cmd Command),
	accept func(conn RedisConn) bool,
	closed func(conn RedisConn, err error),
) error

ListenAndServe creates a new server and binds to addr configured on "tcp" network net.

func ListenAndServeNetwork

func ListenAndServeNetwork(
	net, laddr string,
	handler func(conn RedisConn, cmd Command),
	accept func(conn RedisConn) bool,
	closed func(conn RedisConn, err error),
) error

ListenAndServeNetwork creates a new server and binds to addr. The network net must be a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket"

func ListenAndServeNetworkTLS

func ListenAndServeNetworkTLS(
	net, laddr string,
	handler func(conn RedisConn, cmd Command),
	accept func(conn RedisConn) bool,
	closed func(conn RedisConn, err error),
	config *tls.Config,
) error

ListenAndServeNetworkTLS creates a new TLS server and binds to addr. The network net must be a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket"

func ListenAndServeTLS

func ListenAndServeTLS(addr string,
	handler func(conn RedisConn, cmd Command),
	accept func(conn RedisConn) bool,
	closed func(conn RedisConn, err error),
	config *tls.Config,
) error

ListenAndServeTLS creates a new TLS server and binds to addr configured on "tcp" network net.

Types

type BulkString

type BulkString []byte

func (BulkString) Human

func (d BulkString) Human() string

func (BulkString) Protocol

func (d BulkString) Protocol() string

func (BulkString) Quote

func (d BulkString) Quote() string

func (BulkString) Raw

func (d BulkString) Raw() string

type Command

type Command struct {
	// Raw is a encoded RESP message.
	Raw []byte
	// Args is a series of arguments that make up the command.
	Args [][]byte
}

Command represent a command

func Parse

func Parse(raw []byte) (Command, error)

Parse parses a raw RESP message and returns a command.

type Data

type Data interface {
	// Protocol returns the full Redis protocol representation including prefix and trailing CRLF.
	Protocol() string
	// Raw is the raw string representation. Nil bulk strings return as empty strings.
	Raw() string
	// Human is a human-readable representation.
	Human() string
	// Quote is a single-line, quoted representation. Array elements are quoted individually.
	Quote() string
}

type Decoder

type Decoder struct {
	Err error
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func NewDecoderBuffer

func NewDecoderBuffer(br *bufio2.Reader) *Decoder

func NewDecoderSize

func NewDecoderSize(r io.Reader, size int) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode() (*Resp, error)

func (*Decoder) DecodeMultiBulk

func (d *Decoder) DecodeMultiBulk() ([]*Resp, error)

type DetachedConn

type DetachedConn interface {
	// RedisConn is the original connection
	RedisConn
	// ReadCommand reads the next client command.
	ReadCommand() (Command, error)
	// Flush flushes any writes to the network.
	Flush() error
}

DetachedConn represents a connection that is detached from the server

type Encoder

type Encoder struct {
	Err error
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func NewEncoderBuffer

func NewEncoderBuffer(bw *bufio2.Writer) *Encoder

func NewEncoderSize

func NewEncoderSize(w io.Writer, size int) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(r *Resp, flush bool) error

func (*Encoder) EncodeMultiBulk

func (e *Encoder) EncodeMultiBulk(multi []*Resp, flush bool) error

func (*Encoder) Flush

func (e *Encoder) Flush() error

type Kind

type Kind int

Kind is the kind of command

const (
	// Redis is returned for Redis protocol commands
	Redis Kind = iota
	// Tile38 is returnd for Tile38 native protocol commands
	Tile38
	// Telnet is returnd for plain telnet commands
	Telnet
)

func ReadNextCommand

func ReadNextCommand(packet []byte, argsbuf [][]byte) (
	complete bool, args [][]byte, kind Kind, leftover []byte, err error,
)

ReadNextCommand reads the next command from the provided packet. It's possible that the packet contains multiple commands, or zero commands when the packet is incomplete. 'argsbuf' is an optional reusable buffer and it can be nil. 'complete' indicates that a command was read. false means no more commands. 'args' are the output arguments for the command. 'kind' is the type of command that was read. 'leftover' is any remaining unused bytes which belong to the next command. 'err' is returned when a protocol error was encountered.

type Reader

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

Reader represent a reader for RESP or telnet commands.

func NewReader

func NewReader(rd io.Reader) *Reader

NewReader returns a command reader which will read RESP or telnet commands.

func (*Reader) ReadCommand

func (rd *Reader) ReadCommand() (Command, error)

ReadCommand reads the next command.

type RedisConn

type RedisConn interface {
	// RemoteAddr returns the remote address of the client connection.
	RemoteAddr() string
	// Close closes the connection.
	Close() error
	// WriteError writes an error to the client.
	WriteError(msg string)
	// WriteString writes a string to the client.
	WriteString(str string)
	// WriteBulk writes bulk bytes to the client.
	WriteBulk(bulk []byte)
	// WriteBulkString writes a bulk string to the client.
	WriteBulkString(bulk string)
	// WriteInt writes an integer to the client.
	WriteInt(num int)
	// WriteInt64 writes a 64-but signed integer to the client.
	WriteInt64(num int64)
	// WriteArray writes an array header. You must then write additional
	// sub-responses to the client to complete the response.
	// For example to write two strings:
	//
	//   c.WriteArray(2)
	//   c.WriteBulk("item 1")
	//   c.WriteBulk("item 2")
	WriteArray(count int)
	// WriteNull writes a null to the client
	WriteNull()
	// WriteRaw writes raw data to the client.
	WriteRaw(data []byte)
	// Context returns a user-defined context
	Context() interface{}
	// SetContext sets a user-defined context
	SetContext(v interface{})
	// SetReadBuffer updates the buffer read size for the connection
	SetReadBuffer(bytes int)
	// Detach return a connection that is detached from the server.
	// Useful for operations like PubSub.
	//
	//   dconn := conn.Detach()
	//   go func(){
	//       defer dconn.Close()
	//       cmd, err := dconn.ReadCommand()
	//       if err != nil{
	//           fmt.Printf("read failed: %v\n", err)
	//	         return
	//       }
	//       fmt.Printf("received command: %v", cmd)
	//       hconn.WriteString("OK")
	//       if err := dconn.Flush(); err != nil{
	//           fmt.Printf("write failed: %v\n", err)
	//	         return
	//       }
	//   }()
	Detach() DetachedConn
	// ReadPipeline returns all commands in current pipeline, if any
	// The commands are removed from the pipeline.
	ReadPipeline() []Command
	// PeekPipeline returns all commands in current pipeline, if any.
	// The commands remain in the pipeline.
	PeekPipeline() []Command
	// NetConn returns the base net.Conn connection
	NetConn() net.Conn
}

Conn represents a client connection

type Resp

type Resp struct {
	Type RespType

	Value []byte
	Array []*Resp
}

func Decode

func Decode(r io.Reader) (*Resp, error)

func DecodeFromBytes

func DecodeFromBytes(p []byte) (*Resp, error)

func DecodeMultiBulkFromBytes

func DecodeMultiBulkFromBytes(p []byte) ([]*Resp, error)

func NewArray

func NewArray(array []*Resp) *Resp

func NewBulkBytes

func NewBulkBytes(value []byte) *Resp

func NewError

func NewError(value []byte) *Resp

func NewErrorf

func NewErrorf(format string, args ...interface{}) *Resp

func NewInt

func NewInt(value []byte) *Resp

func NewString

func NewString(value []byte) *Resp

func (*Resp) IsArray

func (r *Resp) IsArray() bool

func (*Resp) IsBulkBytes

func (r *Resp) IsBulkBytes() bool

func (*Resp) IsError

func (r *Resp) IsError() bool

func (*Resp) IsInt

func (r *Resp) IsInt() bool

func (*Resp) IsString

func (r *Resp) IsString() bool

func (*Resp) Key

func (r *Resp) Key() ([]byte, error)

func (*Resp) Op

func (r *Resp) Op() ([]byte, error)

type RespArray

type RespArray []Data

func (RespArray) Human

func (d RespArray) Human() string

func (RespArray) Protocol

func (d RespArray) Protocol() string

func (RespArray) Quote

func (d RespArray) Quote() string

func (RespArray) Raw

func (d RespArray) Raw() string

type RespError

type RespError string

func (RespError) Human

func (d RespError) Human() string

func (RespError) Protocol

func (d RespError) Protocol() string

func (RespError) Quote

func (d RespError) Quote() string

func (RespError) Raw

func (d RespError) Raw() string

type RespInteger

type RespInteger int64

func (RespInteger) Human

func (d RespInteger) Human() string

func (RespInteger) Protocol

func (d RespInteger) Protocol() string

func (RespInteger) Quote

func (d RespInteger) Quote() string

func (RespInteger) Raw

func (d RespInteger) Raw() string

type RespSimpleString

type RespSimpleString string

func (RespSimpleString) Human

func (d RespSimpleString) Human() string

func (RespSimpleString) Protocol

func (d RespSimpleString) Protocol() string

should we validate newline chars?

func (RespSimpleString) Quote

func (d RespSimpleString) Quote() string

func (RespSimpleString) Raw

func (d RespSimpleString) Raw() string

type RespType

type RespType byte
const (
	TypeString    RespType = '+'
	TypeError     RespType = '-'
	TypeInt       RespType = ':'
	TypeBulkBytes RespType = '$'
	TypeArray     RespType = '*'
)

func (RespType) String

func (t RespType) String() string

type Server

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

Server defines a server for clients for managing client connections.

func NewServer

func NewServer(addr string,
	handler func(conn RedisConn, cmd Command),
	accept func(conn RedisConn) bool,
	closed func(conn RedisConn, err error),
) *Server

NewServer returns a new Redcon server configured on "tcp" network net.

func NewServerNetwork

func NewServerNetwork(
	net, laddr string,
	handler func(conn RedisConn, cmd Command),
	accept func(conn RedisConn) bool,
	closed func(conn RedisConn, err error),
) *Server

NewServerNetwork returns a new Redcon server. The network net must be a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket"

func (*Server) Addr

func (s *Server) Addr() net.Addr

Addr returns server's listen address

func (*Server) Close

func (s *Server) Close() error

Close stops listening on the TCP address. Already Accepted connections will be closed.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe serves incoming connections.

func (*Server) ListenServeAndSignal

func (s *Server) ListenServeAndSignal(signal chan error) error

ListenServeAndSignal serves incoming connections and passes nil or error when listening. signal can be nil.

type TLSServer

type TLSServer struct {
	*Server
	// contains filtered or unexported fields
}

TLSServer defines a server for clients for managing client connections.

func NewServerNetworkTLS

func NewServerNetworkTLS(
	net, laddr string,
	handler func(conn RedisConn, cmd Command),
	accept func(conn RedisConn) bool,
	closed func(conn RedisConn, err error),
	config *tls.Config,
) *TLSServer

NewServerNetworkTLS returns a new TLS Redcon server. The network net must be a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket"

func NewServerTLS

func NewServerTLS(addr string,
	handler func(conn RedisConn, cmd Command),
	accept func(conn RedisConn) bool,
	closed func(conn RedisConn, err error),
	config *tls.Config,
) *TLSServer

NewServerTLS returns a new Redcon TLS server configured on "tcp" network net.

func (*TLSServer) Close

func (s *TLSServer) Close() error

Close stops listening on the TCP address. Already Accepted connections will be closed.

func (*TLSServer) ListenAndServe

func (s *TLSServer) ListenAndServe() error

ListenAndServe serves incoming connections.

func (*TLSServer) ListenServeAndSignal

func (s *TLSServer) ListenServeAndSignal(signal chan error) error

ListenServeAndSignal serves incoming connections and passes nil or error when listening. signal can be nil.

type Writer

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

Writer allows for writing RESP messages.

func BaseWriter

func BaseWriter(c RedisConn) *Writer

BaseWriter returns the underlying connection writer, if any

func NewWriter

func NewWriter(wr io.Writer) *Writer

NewWriter creates a new RESP writer.

func (*Writer) Buffer

func (w *Writer) Buffer() []byte

Buffer returns the unflushed buffer. This is a copy so changes to the resulting []byte will not affect the writer.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush writes all unflushed Write* calls to the underlying writer.

func (*Writer) SetBuffer

func (w *Writer) SetBuffer(raw []byte)

SetBuffer replaces the unflushed buffer with new bytes.

func (*Writer) WriteArray

func (w *Writer) WriteArray(count int)

WriteArray writes an array header. You must then write additional sub-responses to the client to complete the response. For example to write two strings:

c.WriteArray(2)
c.WriteBulk("item 1")
c.WriteBulk("item 2")

func (*Writer) WriteBulk

func (w *Writer) WriteBulk(bulk []byte)

WriteBulk writes bulk bytes to the client.

func (*Writer) WriteBulkString

func (w *Writer) WriteBulkString(bulk string)

WriteBulkString writes a bulk string to the client.

func (*Writer) WriteError

func (w *Writer) WriteError(msg string)

WriteError writes an error to the client.

func (*Writer) WriteInt

func (w *Writer) WriteInt(num int)

WriteInt writes an integer to the client.

func (*Writer) WriteInt64

func (w *Writer) WriteInt64(num int64)

WriteInt64 writes a 64-bit signed integer to the client.

func (*Writer) WriteNull

func (w *Writer) WriteNull()

WriteNull writes a null to the client

func (*Writer) WriteRaw

func (w *Writer) WriteRaw(data []byte)

WriteRaw writes raw data to the client.

func (*Writer) WriteString

func (w *Writer) WriteString(msg string)

WriteString writes a string to the client.

Directories

Path Synopsis
utils
log

Jump to

Keyboard shortcuts

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