surely tiny reads explain slowness on command docs

This commit is contained in:
bel
2026-02-04 00:20:37 -07:00
parent 503d078b78
commit d745119615

View File

@@ -32,7 +32,6 @@ func adapt(ctx context.Context, config Config, conn net.Conn) error {
if len(message) > 1 { if len(message) > 1 {
hashKey = message[1].(string) hashKey = message[1].(string)
} }
var reply []byte
if err := config.WithConn(hashKey, func(forwardConn net.Conn) error { if err := config.WithConn(hashKey, func(forwardConn net.Conn) error {
//log.Printf("forwarding %q", raw) //log.Printf("forwarding %q", raw)
@@ -47,22 +46,12 @@ func adapt(ctx context.Context, config Config, conn net.Conn) error {
replyer := bufio.NewReader(forwardConn) replyer := bufio.NewReader(forwardConn)
//log.Printf("reading reply to %q", raw) //log.Printf("reading reply to %q", raw)
reply, _, err = readMessage(replyer) _, err = readMessageTo(conn, replyer)
//log.Printf("read reply %q", raw) //log.Printf("read reply %q", raw)
return err return err
}); err != nil { }); err != nil {
return true, err return true, err
} }
log.Printf("replying: %v", len(reply))
written := 0
for written < len(reply) {
more, err := conn.Write(reply[written:min(len(reply), written+4096)])
if err != nil {
return true, err
}
written += more
//log.Printf("replied %v of %v...", written, len(reply))
}
} }
return false, nil return false, nil
@@ -77,42 +66,42 @@ func adapt(ctx context.Context, config Config, conn net.Conn) error {
} }
func readMessage(reader *bufio.Reader) ([]byte, []any, error) { func readMessage(reader *bufio.Reader) ([]byte, []any, error) {
b, arr, err := _readMessage(reader) w := bytes.NewBuffer(nil)
return b, arr, err arr, err := readMessageTo(w, reader)
return w.Bytes(), arr, err
} }
func _readMessage(reader *bufio.Reader) ([]byte, []any, error) { func readMessageTo(w io.Writer, reader *bufio.Reader) ([]any, error) {
w := bytes.NewBuffer(nil)
firstLine, _, err := reader.ReadLine() firstLine, _, err := reader.ReadLine()
if err != nil { if err != nil {
return w.Bytes(), nil, err return nil, err
} }
firstLine = bytes.TrimSuffix(firstLine, []byte("\r\n")) firstLine = bytes.TrimSuffix(firstLine, []byte("\r\n"))
if len(firstLine) == 0 { if len(firstLine) == 0 {
return w.Bytes(), nil, nil return nil, nil
} }
fmt.Fprintf(w, "%s\r\n", firstLine) fmt.Fprintf(w, "%s\r\n", firstLine)
switch firstLine[0] { switch firstLine[0] {
case '+': // simple string, like +OK case '+': // simple string, like +OK
return w.Bytes(), []any{string(firstLine[1:])}, nil return []any{string(firstLine[1:])}, nil
case '-': // simple error, like -message case '-': // simple error, like -message
return w.Bytes(), []any{string(firstLine)}, nil return []any{string(firstLine)}, nil
case ':': // number, like /[+-][0-9]+/ case ':': // number, like /[+-][0-9]+/
firstLine = bytes.TrimPrefix(firstLine[1:], []byte("+")) firstLine = bytes.TrimPrefix(firstLine[1:], []byte("+"))
n, err := strconv.Atoi(string(firstLine)) n, err := strconv.Atoi(string(firstLine))
if err != nil { if err != nil {
return w.Bytes(), nil, fmt.Errorf("num not a num in %q: %q: %w", w.Bytes(), firstLine, err) return nil, fmt.Errorf("num not a num: %q: %w", firstLine, err)
} }
return w.Bytes(), []any{n}, err return []any{n}, err
case '$': // long string, like $-1 for nil, like $LEN\r\nSTRING\r\n case '$': // long string, like $-1 for nil, like $LEN\r\nSTRING\r\n
if firstLine[1] == '-' { if firstLine[1] == '-' {
return w.Bytes(), []any{nil}, nil return []any{nil}, nil
} }
firstLine = bytes.TrimPrefix(firstLine[1:], []byte("+")) firstLine = bytes.TrimPrefix(firstLine[1:], []byte("+"))
n, err := strconv.Atoi(string(firstLine)) n, err := strconv.Atoi(string(firstLine))
if err != nil { if err != nil {
return w.Bytes(), nil, fmt.Errorf("num not a num in %q: %q: %w", w.Bytes(), firstLine, err) return nil, fmt.Errorf("num not a num: %q: %w", firstLine, err)
} }
//log.Printf("reading %v+2 bytes for bulk string", n) //log.Printf("reading %v+2 bytes for bulk string", n)
nextLine := make([]byte, n+2) nextLine := make([]byte, n+2)
@@ -120,40 +109,36 @@ func _readMessage(reader *bufio.Reader) ([]byte, []any, error) {
for nAt < n+2 { for nAt < n+2 {
nMore, err := reader.Read(nextLine[nAt:]) nMore, err := reader.Read(nextLine[nAt:])
if err != nil { if err != nil {
return w.Bytes(), nil, fmt.Errorf("couldnt read %v more/%v bytes for long string: %w", n+2-nAt, n+2, err) return nil, fmt.Errorf("couldnt read %v more/%v bytes for long string: %w", n+2-nAt, n+2, err)
} }
nAt += nMore nAt += nMore
} }
fmt.Fprintf(w, "%s", nextLine) fmt.Fprintf(w, "%s", nextLine)
return w.Bytes(), []any{string(nextLine)}, err return []any{string(nextLine)}, err
case '*': // array, like *-1 for nil, like *4 for [1,2,3,4] case '*': // array, like *-1 for nil, like *4 for [1,2,3,4]
n, err := strconv.Atoi(string(firstLine[1:])) n, err := strconv.Atoi(string(firstLine[1:]))
if err != nil { if err != nil {
return w.Bytes(), nil, fmt.Errorf("arr not a num: %q: %w", firstLine, err) return nil, fmt.Errorf("arr not a num: %q: %w", firstLine, err)
} else if n == -1 { } else if n == -1 {
return w.Bytes(), nil, nil return nil, nil
} }
var result []any var result []any
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
moreBytes, more, err := _readMessage(reader) more, err := readMessageTo(w, reader)
moreBytes = bytes.TrimSuffix(moreBytes, []byte("\r\n"))
if len(moreBytes) > 0 {
fmt.Fprintf(w, "%s\r\n", moreBytes)
}
if err != nil { if err != nil {
return w.Bytes(), nil, err return nil, err
} }
result = append(result, more...) result = append(result, more...)
} }
return w.Bytes(), result, nil return result, nil
case '_': // nil case '_': // nil
return w.Bytes(), nil, nil return nil, nil
case '#': // boolean, like #t or #f case '#': // boolean, like #t or #f
return w.Bytes(), []any{firstLine[1] == 't'}, nil return []any{firstLine[1] == 't'}, nil
case ',': // double case ',': // double
log.Fatal("not impl") log.Fatal("not impl")
} }
log.Fatalf("not impl: %q", firstLine) log.Fatalf("not impl: %q", firstLine)
return w.Bytes(), nil, nil return nil, nil
} }