refactor adapt
This commit is contained in:
99
src/adapt.go
99
src/adapt.go
@@ -15,73 +15,33 @@ import (
|
||||
func adapt(ctx context.Context, config Config, conn net.Conn) error {
|
||||
reader := bufio.NewReader(conn)
|
||||
|
||||
alpha := regexp.MustCompile(`[a-zA-Z]`)
|
||||
parts := regexp.MustCompile(`(.*?)\r\n*`)
|
||||
|
||||
for ctx.Err() == nil {
|
||||
if done, err := func() (bool, error) {
|
||||
raw, err := readMessage(reader)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return true, nil
|
||||
}
|
||||
return true, err
|
||||
}
|
||||
log.Printf("routing: %q", raw)
|
||||
|
||||
if len(raw) > 0 {
|
||||
hashKey := ""
|
||||
if matches := parts.FindAllSubmatch(raw, 5); len(matches) > 0 {
|
||||
for i := range matches {
|
||||
if isTheCommand := alpha.Match(matches[i][0]); isTheCommand {
|
||||
for j := i + 1; j < len(matches); j++ {
|
||||
if matches[j][0][0] == byte('$') {
|
||||
continue
|
||||
}
|
||||
hashKey = string(matches[j][1])
|
||||
break
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Printf("hashkey %q", hashKey)
|
||||
if err := config.WithConn(hashKey, func(forwardConn net.Conn) error {
|
||||
written := 0
|
||||
for written < len(raw) {
|
||||
more, err := forwardConn.Write(raw[written:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
written += more
|
||||
}
|
||||
|
||||
peekW := bytes.NewBuffer(nil)
|
||||
peek := io.TeeReader(forwardConn, peekW)
|
||||
defer func() {
|
||||
replied := peekW.Bytes()
|
||||
if len(replied) > 100 {
|
||||
replied = []byte(fmt.Sprintf("%s...%s", replied[:50], replied[len(replied)-50:]))
|
||||
}
|
||||
log.Printf("replied %q", replied)
|
||||
}()
|
||||
return readMessageTo(conn, bufio.NewReader(peek))
|
||||
}); err != nil {
|
||||
return true, err
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}(); err != nil {
|
||||
if err := adaptOne(ctx, config, conn, reader); err != nil {
|
||||
return err
|
||||
} else if done {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return io.EOF
|
||||
}
|
||||
|
||||
func adaptOne(ctx context.Context, config Config, conn io.Writer, reader *bufio.Reader) error {
|
||||
raw, err := readMessage(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
log.Printf("routing: %q", raw)
|
||||
|
||||
return config.WithConn(parseHashKey(raw), func(forwardConn net.Conn) error {
|
||||
if _, err := io.Copy(forwardConn, bytes.NewReader(raw)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return readMessageTo(conn, bufio.NewReader(forwardConn))
|
||||
})
|
||||
}
|
||||
|
||||
func readMessage(reader *bufio.Reader) ([]byte, error) {
|
||||
w := bytes.NewBuffer(nil)
|
||||
err := readMessageTo(w, reader)
|
||||
@@ -164,3 +124,24 @@ func _readMessageTo(w io.Writer, reader *bufio.Reader) error {
|
||||
log.Fatalf("not impl: %q", firstLine)
|
||||
return nil
|
||||
}
|
||||
|
||||
var alpha = regexp.MustCompile(`[a-zA-Z]`)
|
||||
var parts = regexp.MustCompile(`(.*?)\r\n*`)
|
||||
|
||||
func parseHashKey(raw []byte) string {
|
||||
matches := parts.FindAllSubmatch(raw, 5)
|
||||
if len(matches) == 0 {
|
||||
return ""
|
||||
}
|
||||
for i := range matches {
|
||||
if isTheCommand := alpha.Match(matches[i][0]); isTheCommand {
|
||||
for j := i + 1; j < len(matches); j++ {
|
||||
if matches[j][0][0] == byte('$') {
|
||||
continue
|
||||
}
|
||||
return string(matches[j][1])
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user