Documentation
¶
Overview ¶
Package redcon implements a Redis compatible server framework
Index ¶
- Constants
- Variables
- func AppendArray(b []byte, n int) []byte
- func AppendBulk(b []byte, bulk []byte) []byte
- func AppendBulkString(b []byte, bulk string) []byte
- func AppendError(b []byte, s string) []byte
- func AppendInt(b []byte, n int64) []byte
- func AppendNull(b []byte) []byte
- func AppendOK(b []byte) []byte
- func AppendString(b []byte, s string) []byte
- func AppendTile38(b []byte, data []byte) []byte
- func AppendUint(b []byte, n uint64) []byte
- func Btoi64(b []byte) (int64, error)
- func Encode(w io.Writer, r *Resp) error
- func EncodeToBytes(r *Resp) ([]byte, error)
- func ListenAndServe(addr string, handler func(conn RedisConn, cmd Command), ...) error
- func ListenAndServeNetwork(net, laddr string, handler func(conn RedisConn, cmd Command), ...) error
- func ListenAndServeNetworkTLS(net, laddr string, handler func(conn RedisConn, cmd Command), ...) error
- func ListenAndServeTLS(addr string, handler func(conn RedisConn, cmd Command), ...) error
- type BulkString
- type Command
- type Data
- type Decoder
- type DetachedConn
- type Encoder
- type Kind
- type Reader
- type RedisConn
- type Resp
- func Decode(r io.Reader) (*Resp, error)
- func DecodeFromBytes(p []byte) (*Resp, error)
- func DecodeMultiBulkFromBytes(p []byte) ([]*Resp, error)
- func NewArray(array []*Resp) *Resp
- func NewBulkBytes(value []byte) *Resp
- func NewError(value []byte) *Resp
- func NewErrorf(format string, args ...interface{}) *Resp
- func NewInt(value []byte) *Resp
- func NewString(value []byte) *Resp
- type RespArray
- type RespError
- type RespInteger
- type RespSimpleString
- type RespType
- type Server
- type TLSServer
- type Writer
- func (w *Writer) Buffer() []byte
- func (w *Writer) Flush() error
- func (w *Writer) SetBuffer(raw []byte)
- func (w *Writer) WriteArray(count int)
- func (w *Writer) WriteBulk(bulk []byte)
- func (w *Writer) WriteBulkString(bulk string)
- func (w *Writer) WriteError(msg string)
- func (w *Writer) WriteInt(num int)
- func (w *Writer) WriteInt64(num int64)
- func (w *Writer) WriteNull()
- func (w *Writer) WriteRaw(data []byte)
- func (w *Writer) WriteString(msg string)
Constants ¶
const ( MaxBulkBytesLen = 1024 * 1024 * 512 MaxArrayLen = 1024 * 1024 )
Variables ¶
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") )
var ErrFailedDecoder = errors.New("use of failed decoder")
var ErrFailedEncoder = errors.New("use of failed encoder")
var (
ErrInvalid = errors.New("invalid redis packet")
)
Functions ¶
func AppendArray ¶
AppendArray appends a Redis protocol array to the input bytes.
func AppendBulk ¶
AppendBulk appends a Redis protocol bulk byte slice to the input bytes.
func AppendBulkString ¶
AppendBulkString appends a Redis protocol bulk string to the input bytes.
func AppendError ¶
AppendError appends a Redis protocol error to the input bytes.
func AppendNull ¶
AppendNull appends a Redis protocol null to the input bytes.
func AppendString ¶
AppendString appends a Redis protocol string to the input bytes.
func AppendTile38 ¶
AppendTile38 appends a Tile38 message to the input bytes.
func AppendUint ¶
AppendUint appends a Redis protocol uint64 to the input bytes.
func EncodeToBytes ¶
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"
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
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 NewDecoderBuffer ¶
func (*Decoder) DecodeMultiBulk ¶
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 NewEncoderBuffer ¶
func (*Encoder) EncodeMultiBulk ¶
type Kind ¶
type Kind int
Kind is the kind of command
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 (*Reader) ReadCommand ¶
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 ¶
func DecodeFromBytes ¶
func NewBulkBytes ¶
func (*Resp) IsBulkBytes ¶
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 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) Close ¶
Close stops listening on the TCP address. Already Accepted connections will be closed.
func (*Server) ListenAndServe ¶
ListenAndServe serves incoming connections.
func (*Server) ListenServeAndSignal ¶
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 ¶
Close stops listening on the TCP address. Already Accepted connections will be closed.
func (*TLSServer) ListenAndServe ¶
ListenAndServe serves incoming connections.
func (*TLSServer) ListenServeAndSignal ¶
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 ¶
BaseWriter returns the underlying connection writer, if any
func (*Writer) Buffer ¶
Buffer returns the unflushed buffer. This is a copy so changes to the resulting []byte will not affect the writer.
func (*Writer) WriteArray ¶
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) WriteBulkString ¶
WriteBulkString writes a bulk string to the client.
func (*Writer) WriteError ¶
WriteError writes an error to the client.
func (*Writer) WriteInt64 ¶
WriteInt64 writes a 64-bit signed integer to the client.
func (*Writer) WriteString ¶
WriteString writes a string to the client.