From a69dc46609c26fd680720d4728009f307fba0ce5 Mon Sep 17 00:00:00 2001 From: Xu Xinran Date: Sat, 3 Dec 2016 22:51:26 +0800 Subject: [PATCH] bugfix: use same bufio.Reader for all requests in one connection. Old code create bufio.Reader for each request. When using pipeline mode, bufio.Reader may eat some input data which belong to next request. --- parser.go | 5 +---- request.go | 6 +----- server.go | 4 +++- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/parser.go b/parser.go index caaf49c..62789e2 100644 --- a/parser.go +++ b/parser.go @@ -8,8 +8,7 @@ import ( "strings" ) -func parseRequest(conn io.ReadCloser) (*Request, error) { - r := bufio.NewReader(conn) +func parseRequest(r *bufio.Reader) (*Request, error) { // first line of redis request should be: // *CRLF line, err := r.ReadString('\n') @@ -43,7 +42,6 @@ func parseRequest(conn io.ReadCloser) (*Request, error) { return &Request{ Name: strings.ToLower(string(firstArg)), Args: args, - Body: conn, }, nil } @@ -59,7 +57,6 @@ func parseRequest(conn io.ReadCloser) (*Request, error) { return &Request{ Name: strings.ToLower(string(fields[0])), Args: args, - Body: conn, }, nil } diff --git a/request.go b/request.go index 4888e69..dd16048 100644 --- a/request.go +++ b/request.go @@ -1,16 +1,12 @@ package redis -import ( - "io" - "strconv" -) +import "strconv" type Request struct { Name string Args [][]byte Host string ClientChan chan struct{} - Body io.ReadCloser } func (r *Request) HasArgument(index int) bool { diff --git a/server.go b/server.go index c4a4131..14a1f4c 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ package redis import ( + "bufio" "fmt" "io" "io/ioutil" @@ -88,8 +89,9 @@ func (srv *Server) ServeClient(conn net.Conn) (err error) { clientAddr = co.RemoteAddr().String() } + br := bufio.NewReader(conn) for { - request, err := parseRequest(conn) + request, err := parseRequest(br) if err != nil { return err }