Don't use fmt.Sprintf when sending size of a redis bulk string.

This commit is contained in:
Sascha L. Teichmann 2015-05-27 16:48:51 +02:00
parent a165bcd53a
commit f80d9e452c

View File

@ -6,9 +6,9 @@ package main
import ( import (
"bufio" "bufio"
"fmt"
"log" "log"
"net" "net"
"strconv"
) )
var ( var (
@ -121,7 +121,7 @@ func (c *Connection) Hkeys(hash []byte) bool {
return c.writeEmptyArray() return c.writeEmptyArray()
} }
if _, err := c.conn.Write([]byte(fmt.Sprintf("*%d\r\n", n))); err != nil { if _, err := c.conn.Write(redisLength('*', n)); err != nil {
logError(err) logError(err)
return false return false
} }
@ -204,8 +204,13 @@ func (c *Connection) writeBool(b bool) bool {
return true return true
} }
func redisLength(prefix byte, s int) []byte {
buf := append(make([]byte, 0, 16), prefix)
return append(strconv.AppendInt(buf, int64(s), 10), '\r', '\n')
}
func (c *Connection) writeBoolArray(arr []bool) bool { func (c *Connection) writeBoolArray(arr []bool) bool {
if _, err := c.conn.Write([]byte(fmt.Sprintf("*%d\r\n", len(arr)))); err != nil { if _, err := c.conn.Write(redisLength('*', len(arr))); err != nil {
logError(err) logError(err)
return false return false
} }
@ -246,7 +251,7 @@ func (c *Connection) writeBulkString(data []byte) (err error) {
if data == nil { if data == nil {
_, err = con.Write(redisNoSuchBlock) _, err = con.Write(redisNoSuchBlock)
} else { } else {
if _, err = con.Write([]byte(fmt.Sprintf("$%d\r\n", len(data)))); err != nil { if _, err = con.Write(redisLength('$', len(data))); err != nil {
return return
} }
if _, err = con.Write(data); err != nil { if _, err = con.Write(data); err != nil {