Documentation ¶
Overview ¶
Package red implements server speaking redis serialization protocol.
Example ¶
package main import ( "bytes" "fmt" "io" "net" "os" "github.com/artyom/red" ) func main() { srv := red.NewServer() srv.Handle("ping", func(req red.Request) (interface{}, error) { if len(req.Args) > 0 { return req.Args[0], nil } return "PONG", nil }) ln, err := net.Listen("tcp", "localhost:0") if err != nil { fmt.Println(err) return } defer ln.Close() go func() { srv.Serve(ln) }() conn, err := net.Dial("tcp", ln.Addr().String()) if err != nil { fmt.Println(err) return } // See https://redis.io/topics/protocol // this matches sequence of the following commands run with redis-cli // tool: // // PING "Hello, world" // QUIT fmt.Fprintf(conn, "*2\r\n$4\r\nPING\r\n") fmt.Fprintf(conn, "$12\r\nHello, world\r\n") fmt.Fprintf(conn, "*1\r\n$4\r\nQUIT\r\n") buf := new(bytes.Buffer) io.Copy(buf, conn) os.Stdout.Write(bytes.Replace(buf.Bytes(), []byte{'\r'}, []byte{}, -1)) }
Output: $12 Hello, world +OK
Index ¶
- Variables
- type CmdCount
- type HandlerFunc
- type Logger
- type Request
- type Server
- func (s *Server) Handle(name string, h HandlerFunc)
- func (s *Server) HandleConn(conn io.ReadWriteCloser) error
- func (s *Server) ListenAndServe(addr string) error
- func (s *Server) Serve(l net.Listener) error
- func (s *Server) Stats() []CmdCount
- func (s *Server) WithCommands()
- func (s *Server) WithLogger(l Logger)
- func (s *Server) WithUnsafeTx()
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrWrongArgs = errors.New("wrong number of arguments")
ErrWrongArgs are expected to be returned by HandlerFunc implementations when number of arguments are wrong. This error is automatically passed to client with command name annotated.
Functions ¶
This section is empty.
Types ¶
type CmdCount ¶ added in v0.1.0
CmdCount describes the number of times a particular command was processed
type HandlerFunc ¶
HandlerFunc is a type of command processing function. It should return value that's passed to resp.Encode function, so it's expected such function would return values of resp package types.
type Logger ¶
type Logger interface { Print(v ...interface{}) Printf(format string, v ...interface{}) Println(v ...interface{}) }
Logger is a set of methods used to log information. *log.Logger implements this interface.
type Request ¶
type Request struct { Name string // lowercase command itself (first word) Args []string // command arguments }
Request holds information about single redis command.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server implements server speaking RESP (REdis Serialization Protocol). Server automatically handles MULTI, EXEC, DISCARD commands for transactions, QUIT for client-initiated disconnect, other commands are expected to be implemented separately and registered with Handle method.
func (*Server) Handle ¶
func (s *Server) Handle(name string, h HandlerFunc)
Handle registers handler for command with given name (case-insensitive)
func (*Server) HandleConn ¶
func (s *Server) HandleConn(conn io.ReadWriteCloser) error
HandleConn processes single client connection, automatically handling following commands MULTI/EXEC/DISCARD (transactions), QUIT (client disconnect). It calls user-provided handlers for registered commands.
func (*Server) ListenAndServe ¶
ListenAndServe listens on TCP network address addr and then calls Serve to handle requests on incoming connections.
func (*Server) Serve ¶
Serve accepts incoming connections on the Listener l, creating a new service goroutine for each.
func (*Server) Stats ¶ added in v0.1.0
Stats returns statistics about the number of known command calls processed since the previous Stats call.
func (*Server) WithCommands ¶
func (s *Server) WithCommands()
WithCommands enables logging of incoming commands. Logging should be enabled using WithLogger
func (*Server) WithLogger ¶
WithLogger configures server to use provided Logger.
func (*Server) WithUnsafeTx ¶
func (s *Server) WithUnsafeTx()
WithUnsafeTx disables command processing serialization that guarantees redis-like transaction safety.