gredcon

package
v0.0.0-...-8636779 Latest Latest
Warning

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

Go to latest
Published: May 17, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Integer = ':'
	String  = '+'
	Bulk    = '$'
	Array   = '*'
	Error   = '-'
)

Various RESP kinds

Variables

This section is empty.

Functions

func AppendAny

func AppendAny(b []byte, v interface{}) []byte

AppendAny appends any type to valid Redis type.

nil             -> null
error           -> error (adds "ERR " when first word is not uppercase)
string          -> bulk-string
numbers         -> bulk-string
[]byte          -> bulk-string
bool            -> bulk-string ("0" or "1")
slice           -> array
map             -> array with key/value pairs
SimpleString    -> string
SimpleInt       -> integer
Marshaler       -> raw bytes
everything-else -> bulk-string representation using fmt.Sprint()

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 AppendBulkFloat

func AppendBulkFloat(dst []byte, f float64) []byte

AppendBulkFloat appends a float64, as bulk bytes.

func AppendBulkInt

func AppendBulkInt(dst []byte, x int64) []byte

AppendBulkInt appends an int64, as bulk bytes.

func AppendBulkString

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

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

func AppendBulkUint

func AppendBulkUint(dst []byte, x uint64) []byte

AppendBulkUint appends an uint64, as bulk 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 ListenAndServe

func ListenAndServe(options Options, handler func(conn Conn, cmd Command))

Types

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 ReadCommands

func ReadCommands(buf []byte) ([]Command, []byte, error)

ReadCommands parses a raw message and returns commands.

type Conn

type Conn interface {
	// 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-bit signed integer to the client.
	WriteInt64(num int64)
	// WriteUint64 writes a 64-bit unsigned integer to the client.
	WriteUint64(num uint64)
	// 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.WriteBulkString("item 1")
	//   c.WriteBulkString("item 2")
	WriteArray(count int)
	// WriteNull writes a null to the client
	WriteNull()
	// WriteRaw writes raw data to the client.
	WriteRaw(data []byte)
	// WriteAny writes any type to the client.
	//   nil             -> null
	//   error           -> error (adds "ERR " when first word is not uppercase)
	//   string          -> bulk-string
	//   numbers         -> bulk-string
	//   []byte          -> bulk-string
	//   bool            -> bulk-string ("0" or "1")
	//   slice           -> array
	//   map             -> array with key/value pairs
	//   SimpleString    -> string
	//   SimpleInt       -> integer
	//   everything-else -> bulk-string representation using fmt.Sprint()
	WriteAny(any interface{})
	// 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)

	// 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
}

type Handler

type Handler interface {
	ServeRESP(conn Conn, cmd Command)
}

type HandlerFunc

type HandlerFunc func(conn Conn, cmd Command)

func (HandlerFunc) ServeRESP

func (f HandlerFunc) ServeRESP(conn Conn, cmd Command)

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 Marshaler

type Marshaler interface {
	MarshalRESP() []byte
}

Marshaler is the interface implemented by types that can marshal themselves into a Redis response type from an *Any call. The return value is not check for validity.

type Options

type Options struct {
	gnet.Options
	Port int
}

type RESP

type RESP struct {
	Type  Type
	Raw   []byte
	Data  []byte
	Count int
}

RESP ...

func ReadNextRESP

func ReadNextRESP(b []byte) (n int, resp RESP)

ReadNextRESP returns the next resp in b and returns the number of bytes the took up the result.

func (*RESP) ForEach

func (r *RESP) ForEach(iter func(resp RESP) bool)

ForEach iterates over each Array element

type Reader

type Reader struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

func NewReader

func NewReader() *Reader

type RedCon

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

func NewRedcon

func NewRedcon() *RedCon

func (*RedCon) Context

func (c *RedCon) Context() interface{}

func (*RedCon) PeekPipeline

func (c *RedCon) PeekPipeline() []Command

func (*RedCon) ReadPipeline

func (c *RedCon) ReadPipeline() []Command

func (*RedCon) SetContext

func (c *RedCon) SetContext(v interface{})

func (*RedCon) SetReadBuffer

func (c *RedCon) SetReadBuffer(n int)

func (*RedCon) WriteAny

func (c *RedCon) WriteAny(v interface{})

func (*RedCon) WriteArray

func (c *RedCon) WriteArray(count int)

func (*RedCon) WriteBulk

func (c *RedCon) WriteBulk(bulk []byte)

func (*RedCon) WriteBulkString

func (c *RedCon) WriteBulkString(bulk string)

func (*RedCon) WriteError

func (c *RedCon) WriteError(msg string)

func (*RedCon) WriteInt

func (c *RedCon) WriteInt(num int)

func (*RedCon) WriteInt64

func (c *RedCon) WriteInt64(num int64)

func (*RedCon) WriteNull

func (c *RedCon) WriteNull()

func (*RedCon) WriteRaw

func (c *RedCon) WriteRaw(data []byte)

func (*RedCon) WriteString

func (c *RedCon) WriteString(str string)

func (*RedCon) WriteUint64

func (c *RedCon) WriteUint64(num uint64)

type ServeMux

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

func NewServeMux

func NewServeMux() *ServeMux

func (*ServeMux) Handle

func (m *ServeMux) Handle(command string, handler Handler)

func (*ServeMux) HandleFunc

func (m *ServeMux) HandleFunc(command string, handler func(conn Conn, cmd Command))

func (*ServeMux) ServeRESP

func (m *ServeMux) ServeRESP(conn Conn, cmd Command)

type Server

type Server struct {
	*gnet.EventServer

	// AcceptError is an optional function used to handle Accept errors.
	AcceptError func(err error)
	// contains filtered or unexported fields
}

func (*Server) OnClosed

func (s *Server) OnClosed(c gnet.Conn, err error) (action gnet.Action)

func (*Server) OnInitComplete

func (s *Server) OnInitComplete(srv gnet.Server) (action gnet.Action)

func (*Server) OnOpened

func (s *Server) OnOpened(c gnet.Conn) (out []byte, action gnet.Action)

func (*Server) OnShutdown

func (s *Server) OnShutdown(svr gnet.Server)

func (*Server) OnTick

func (s *Server) OnTick() (delay time.Duration, action gnet.Action)

func (*Server) React

func (s *Server) React(frame []byte, conn gnet.Conn) (out []byte, action gnet.Action)

type SimpleInt

type SimpleInt int

SimpleInt is for representing a non-bulk representation of a int from an *Any call.

type SimpleString

type SimpleString string

SimpleString is for representing a non-bulk representation of a string from an *Any call.

type Type

type Type byte

Type of RESP

type Writer

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

Writer allows for writing RESP messages.

func NewWriter

func NewWriter() *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) WriteAny

func (w *Writer) WriteAny(v interface{})

WriteAny writes any type to client.

nil             -> null
error           -> error (adds "ERR " when first word is not uppercase)
string          -> bulk-string
numbers         -> bulk-string
[]byte          -> bulk-string
bool            -> bulk-string ("0" or "1")
slice           -> array
map             -> array with key/value pairs
SimpleString    -> string
SimpleInt       -> integer
everything-else -> bulk-string representation using fmt.Sprint()

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.

func (*Writer) WriteUint64

func (w *Writer) WriteUint64(num uint64)

WriteUint64 writes a 64-bit unsigned integer to the client.

Jump to

Keyboard shortcuts

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