Documentation ¶
Overview ¶
Package resp3 implements redis RESP3 protocol, which is used from redis 6.0.
RESP (REdis Serialization Protocol) is the protocol used in the Redis database, however the protocol is designed to be used by other projects. With the version 3 of the protocol, currently a work in progress design, the protocol aims to become even more generally useful to other systems that want to implement a protocol which is simple, efficient, and with a very large landscape of client libraries implementations. That means you can use this library to access other RESP3 projects.
This library contains three important components: Value, Reader and Writer.
Value represents a redis command or a redis response. It is a common struct for all RESP3 types.
Reader can parse redis responses from redis servers or commands from redis clients. You can use it to implement redis 6.0 clients, no need to pay attention to underlying parsing. Those new features of redis 6.0 can be implemented based on it.
Writer is redis writer. You can use it to send commands to redis servers.
RESP3 spec can be found at https://github.com/antirez/RESP3.
A redis client based on it is just as the below:
conn, err := net.DialTimeout("tcp", "127.0.0.1:6379", 5*time.Second) if err != nil { t.Logf("can't found one of redis 6.0 server") return } defer conn.Close() w := NewWriter(conn) r := NewReader(conn) // send protocol version 3, get a map result w.WriteCommand("HELLO", "3") resp, _, _ := r.ReadValue() log.Printf("%v", resp.SmartResult()) // set w.WriteCommand("SET", "A", "123") resp, _, _ := r.ReadValue() log.Printf("%v", resp.SmartResult()) // get w.WriteCommand("GET", "A") resp, _, _ = r.ReadValue() log.Printf("%v", resp.SmartResult())
Index ¶
Constants ¶
const ( TypeBlobString = '$' // $<length>\r\n<bytes>\r\n TypeSimpleString = '+' // +<string>\r\n TypeSimpleError = '-' // -<string>\r\n TypeNumber = ':' // :<number>\r\n TypeNull = '_' // _\r\n TypeDouble = ',' // ,<floating-point-number>\r\n TypeBoolean = '#' // #t\r\n or #f\r\n TypeBlobError = '!' // !<length>\r\n<bytes>\r\n TypeVerbatimString = '=' // =<length>\r\n<format(3 bytes):><bytes>\r\n TypeBigNumber = '(' // (<big number>\n TypeArray = '*' // *<elements number>\r\n... numelements other types ... TypeMap = '%' // %<elements number>\r\n... numelements key/value pair of other types ... TypeSet = '~' // ~<elements number>\r\n... numelements other types ... TypeAttribute = '|' // |~<elements number>\r\n... numelements map type ... TypePush = '>' // ><elements number>\r\n<first item is String>\r\n... numelements-1 other types ... //special type TypeStream = "$EOF:" // $EOF:<40 bytes marker><CR><LF>... any number of bytes of data here not containing the marker ...<40 bytes marker> )
resp3 type char
const CRLF = "\r\n"
const TRACKING_TABLE_SIZE = 1 << 24
Variables ¶
var ( ErrInvalidSyntax = errors.New("resp: invalid syntax") ErrStreamingUnsupport = errors.New("resp: unsupported streaming") ErrUnknown = errors.New("resp: unknown") )
Errors
var CRLFByte = []byte(CRLF)
var StreamMarkerPrefix = []byte("$EOF:")
Functions ¶
Types ¶
type Reader ¶
Reader is reader to parse responses/requests from the underlying reader.
func NewReaderSize ¶
NewReaderSize returns a new Reader whose buffer has at least the specified size.
type Value ¶
type Value struct { Type byte Str string StrFmt string Err string Integer int64 Boolean bool Double float64 BigInt *big.Int Elems []*Value // for array & set KV *linkedhashmap.Map //TODO sorted map, for map & attr Attrs *linkedhashmap.Map StreamMarker string }
Value is a common struct for all RESP3 type. There is no exact field for NULL type because the type field is enough. It is not used for Stream type.
func FromString ¶
FromString convert a string into a Value.
func (*Value) SmartResult ¶
func (r *Value) SmartResult() interface{}
SmartResult converts itself to a real object. Attributes are dropped. simple objects are converted their Go types. String -> go string Interger -> go int64 Double -> go float64 Boolean -> go bool Err -> go string BigInt -> big.Int Array -> go array Map --> github.com/emirpasic/gods/maps/linkedhashmap.Map Set -> go array Push -> go array NULL -> nil
func (*Value) ToRESP3String ¶
ToRESP3String converts this value to redis RESP3 string.
type Writer ¶
Writer is a redis client writer. he RESP3 protocol can be used asymmetrically, as it is in Redis: only a subset can be sent by the client to the server, while the server can return the full set of types available. This is due to the fact that RESP is designed to send non structured commands like SET mykey somevalue or SADD myset a b c d. Such commands can be represented as arrays, where each argument is an array element, so this is the only type the client needs to send to a server.
func (*Writer) SendCommands ¶
func (*Writer) WriteByteCommand ¶
WriteByteCommand writes a redis command in bytes.
func (*Writer) WriteCommand ¶
WriteCommand writes a redis command.