Skip to content

Commit

Permalink
Use a pool for bufio.NewReaders to fix perf on ftp getMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
joshrendek committed Oct 24, 2024
1 parent 8d8acd8 commit 2f64b95
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions ftp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/exec"
"path"
"strings"
"sync"
"time"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -212,21 +213,25 @@ func readPortData(ch *ConnectionConfig, username string, out net.Conn) {
}
}

var bufioReaderPool = sync.Pool{
New: func() interface{} {
return bufio.NewReader(nil)
},
}

func getMsg(conn net.Conn) string {
// Split the response into CMD and ARGS
bufc := bufio.NewReader(conn)
for {
line, err := bufc.ReadString('\n')
if err != nil {
conn.Close()
break
}
fmt.Printf("Received: %s\n", line)
return strings.TrimRight(line, "\r")
bufc := bufioReaderPool.Get().(*bufio.Reader)
bufc.Reset(conn)
defer bufioReaderPool.Put(bufc)

line, err := bufc.ReadString('\n')
if err != nil {
conn.Close()
return ""
}
return ""
//fmt.Printf("Received: %s\n", line)
return strings.TrimRight(line, "\r")
}

func sendMsg(c net.Conn, message string) {
//fmt.Printf("Sending: %s\n", message)
io.WriteString(c, message)
Expand Down

0 comments on commit 2f64b95

Please sign in to comment.