diff --git a/rsload.go b/rsload.go index 403a15e..6a125c4 100644 --- a/rsload.go +++ b/rsload.go @@ -80,15 +80,20 @@ func main() { sent := make(chan value, options.buffer) go streamValues(infile, c) go func() { - w := bufio.NewWriterSize(conn, 4048) + w := bufio.NewWriterSize(conn, 16384) defer func() { close(sent) fmt.Println("All data transferred. Waiting for the last reply...") }() + count := 0 for r := range c { + count++ if r.ok() { r.val().Write(w) - w.Flush() + if count == 100 { + w.Flush() + count = 0 + } sent <- r.val() } else { fmt.Fprintf(os.Stderr, "InputError: %v\n", r.err()) diff --git a/values.go b/values.go index 08c212e..d0a20dd 100644 --- a/values.go +++ b/values.go @@ -125,7 +125,11 @@ func readString(b []byte) (value, error) { } func (s String) Write(w io.Writer) (int, error) { - return fmt.Fprintf(w, "+%s\r\n", s) + w.Write([]byte{'+'}) + w.Write([]byte(s)) + w.Write([]byte{'\r', '\n'}) + return 0, nil + // return fmt.Fprintf(w, "+%s\r\n", s) } // ------------------------------------------------------------------------------ @@ -137,7 +141,11 @@ func readError(b []byte) (value, error) { } func (e Error) Write(w io.Writer) (int, error) { - return fmt.Fprintf(w, "-%s\r\n") + w.Write([]byte{'-'}) + w.Write([]byte(e)) + w.Write([]byte{'\r', '\n'}) + return 0, nil + // return fmt.Fprintf(w, "-%s\r\n") } // ------------------------------------------------------------------------------ @@ -153,7 +161,11 @@ func readInteger(b []byte) (value, error) { } func (i Integer) Write(w io.Writer) (int, error) { - return fmt.Fprintf(w, ":%d\r\n", i) + w.Write([]byte{':'}) + w.Write([]byte(strconv.Itoa(int(i)))) + w.Write([]byte{'\r', '\n'}) + return 0, nil + // return fmt.Fprintf(w, ":%d\r\n", i) } // ------------------------------------------------------------------------------ @@ -198,7 +210,13 @@ func readBulkString(prefix []byte, r io.Reader) (value, error) { } func (s BulkString) Write(w io.Writer) (int, error) { - return fmt.Fprintf(w, "$%d\r\n%s\r\n", len(s), s) + w.Write([]byte{'$'}) + w.Write([]byte(strconv.Itoa(len(s)))) + w.Write([]byte{'\r', '\n'}) + w.Write([]byte(s)) + w.Write([]byte{'\r', '\n'}) + return 0, nil + // return fmt.Fprintf(w, "$%d\r\n%s\r\n", len(s), s) } // ----------------------------------------------------------------------------------------- @@ -230,21 +248,26 @@ func readArray(prefix []byte, r *bufio.Reader) (value, error) { } func (a Array) Write(w io.Writer) (int, error) { - n, err := fmt.Fprintf(w, "*%d\r\n", len(a)) - if err != nil { - return n, err - } - - var ( - nn int - e error - ) + w.Write([]byte{'*'}) + w.Write([]byte(strconv.Itoa(len(a)))) + w.Write([]byte{'\r', '\n'}) + // n, err := fmt.Fprintf(w, "*%d\r\n", len(a)) + // if err != nil { + // return n, err + // } + + // var ( + // nn int + // e error + // ) for i := 0; i < len(a); i++ { - nn, e = a[i].Write(w) - n += nn - if e != nil { - return n, e - } + a[i].Write(w) + // nn, e = a[i].Write(w) + // n += nn + // if e != nil { + // return n, e + // } } - return n, nil + // return n, nil + return 0, nil }