Documentation
¶
Overview ¶
Package redcon implements a Redis compatible server framework
Index ¶
- func ListenAndServe(addr string, handler func(conn Conn, cmd Command), accept func(conn Conn) bool, ...) error
- func ListenAndServeNetwork(net, laddr string, handler func(conn Conn, cmd Command), ...) error
- type Command
- type Conn
- type DetachedConn
- type Reader
- type Server
- 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 ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ListenAndServe ¶
func ListenAndServe(addr string, handler func(conn Conn, cmd Command), accept func(conn Conn) bool, closed func(conn Conn, 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 Conn, cmd Command), accept func(conn Conn) bool, closed func(conn Conn, err error), ) error
ListenAndServe creates a new server and binds to addr. The network net must be a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket"
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
type Conn ¶
type Conn 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 addtional // 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 // Flush flushes any writes to the network. Flush() error }
Conn represents a client connection
type DetachedConn ¶
type DetachedConn interface { // Conn is the original connection Conn // ReadCommand reads the next client command. ReadCommand() (Command, error) }
DetachedConn represents a connection that is detached from the server
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.
func (*Reader) ReadCommands ¶
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 Conn, cmd Command), accept func(conn Conn) bool, closed func(conn Conn, err error), ) *Server
NewServer returns a new Redcon server configured on "tcp" network net.
func NewServerNetwork ¶
func NewServerNetwork( net, laddr string, handler func(conn Conn, cmd Command), accept func(conn Conn) bool, closed func(conn Conn, err error), ) *Server
NewServerNetworkType 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.
func (*Server) SetIdleClose ¶ added in v0.9.2
SetIdleClose will automatically close idle connections after the specified duration. Use zero to disable this feature.
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 addtional 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.