proto

package
v0.0.0-...-a319e5f Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2015 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

This package provides an easy to use interface for creating and parsing messages encoded in the REdis Serialization Protocol (RESP). You can check out more details about the protocol here: http://redis.io/topics/protocol

Index

Constants

View Source
const (
	RaftProto = 0
	RpcProto  = 1
)

Variables

View Source
var (
	ErrListenerClosed = errors.New("listener closed")
)
View Source
var (
	ErrNoLeader = errors.New("no leader")
)

Functions

func ByteMatcher

func ByteMatcher(b byte) cmux.Matcher

func DialMsgpack

func DialMsgpack(addr string, timeout time.Duration) (*rpc.Client, error)

func Marshal

func Marshal(msg interface{}) ([]byte, error)

func ServeMsgpack

func ServeMsgpack(l net.Listener, s *rpc.Server) error

func ServeRedis

func ServeRedis(l net.Listener, s *rpc.Server) error

func Unmarshal

func Unmarshal(buf []byte, msg interface{}) error

func WriteArbitrary

func WriteArbitrary(w io.Writer, m interface{}) error

WriteArbitrary takes in any primitive golang value, or Message, and writes its encoded form to the given io.Writer, inferring types where appropriate.

func WriteArbitraryAsFlattenedStrings

func WriteArbitraryAsFlattenedStrings(w io.Writer, m interface{}) error

WriteArbitraryAsFlattenedStrings is similar to WriteArbitrary except that it will encode all types except Array as a BulkStr, converting the argument into a string first as necessary. It will also flatten any embedded arrays into a single long array. This is useful because commands to a redis server must be given as an array of bulk strings. If the argument isn't already in a slice or map it will be wrapped so that it is written as an Array of size one.

Note that if a Message type is found it will *not* be encoded to a BulkStr, but will simply be passed through as whatever type it already represents.

func WriteArbitraryAsString

func WriteArbitraryAsString(w io.Writer, m interface{}) error

WriteArbitraryAsString is similar to WriteArbitraryAsFlattenedString except that it won't flatten any embedded arrays.

func WriteMessage

func WriteMessage(w io.Writer, m *Message) error

WriteMessage takes in the given Message and writes its encoded form to the given io.Writer

Types

type Action

type Action int
const (
	OpPing Action = iota
	OpRead
	OpWrite
	OpDelete
)

type Item

type Item struct {
	Key  []byte
	Data []byte
}

type Message

type Message struct {
	Type
	// contains filtered or unexported fields
}

func NewMessage

func NewMessage(b []byte) (*Message, error)

NewMessagePParses the given raw message and returns a Message struct representing it

func NewSimpleString

func NewSimpleString(s string) *Message

Can be used when writing to a resp stream to write a simple-string-style stream (e.g. +OK\r\n) instead of the default bulk-string-style strings.

foo := NewSimpleString("foo")
bar := NewSimpleString("bar")
baz := NewSimpleString("baz")
resp.WriteArbitrary(w, foo)
resp.WriteArbitrary(w, []interface{}{bar, baz})

func ReadMessage

func ReadMessage(reader io.Reader) (*Message, error)

ReadMessage attempts to read a message object from the given io.Reader, parse it, and return a Message struct representing it

func (*Message) Array

func (m *Message) Array() ([]*Message, error)

Array returns the Message slice encompassed by this Messsage, assuming the Message is of type Array

func (*Message) Bytes

func (m *Message) Bytes() ([]byte, error)

Bytes returns a byte slice representing the value of the Message. Only valid for a Message of type SimpleStr, Err, and BulkStr. Others will return an error

func (*Message) Err

func (m *Message) Err() (error, error)

Err returns an error representing the value of the Message. Only valid for Err messages

func (*Message) Int

func (m *Message) Int() (int64, error)

Int returns an int64 representing the value of the Message. Only valid for Int messages

func (*Message) Raw

func (m *Message) Raw() []byte

func (*Message) Str

func (m *Message) Str() (string, error)

Str is a Convenience method around Bytes which converts the output to a string

type Mux

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

func NewMux

func NewMux(l net.Listener, log log15.Logger) *Mux

func (*Mux) Close

func (m *Mux) Close() error

func (*Mux) Handle

func (m *Mux) Handle(proto byte) net.Listener

Handle register a Listener to Mux When a connection reached, Mux will compare first byte to proto, if equal, the corresponding Listener Note: the first byte will consume from connection

func (*Mux) HandleThird

func (m *Mux) HandleThird(matcher ...cmux.Matcher) net.Listener

Default will not comsume first byte

func (*Mux) Serve

func (m *Mux) Serve() error

type RedisServerCodec

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

func NewRedisServerCodec

func NewRedisServerCodec(conn net.Conn) *RedisServerCodec

func (*RedisServerCodec) Close

func (c *RedisServerCodec) Close() error

func (*RedisServerCodec) ReadRequestBody

func (c *RedisServerCodec) ReadRequestBody(x interface{}) error

func (*RedisServerCodec) ReadRequestHeader

func (c *RedisServerCodec) ReadRequestHeader(r *rpc.Request) error

func (*RedisServerCodec) WriteResponse

func (c *RedisServerCodec) WriteResponse(r *rpc.Response, x interface{}) error

type Reply

type Reply struct {
	Data []byte
}

type Request

type Request struct {
	Action Action
	Key    []byte
	Data   []byte
}

type Type

type Type int
const (
	SimpleStr Type = iota
	Err
	Int
	BulkStr
	Array
	Nil
)

Jump to

Keyboard shortcuts

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