Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protocol: avoid double buffering #116

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ type Conn struct {
once sync.Once
readErr error
conn net.Conn
Validate Validator
bufReader *bufio.Reader
reader io.Reader
header *Header
ProxyHeaderPolicy Policy
Validate Validator
readHeaderTimeout time.Duration
}

Expand Down Expand Up @@ -134,8 +135,15 @@ func (p *Listener) Addr() net.Addr {
// NewConn is used to wrap a net.Conn that may be speaking
// the proxy protocol into a proxyproto.Conn
func NewConn(conn net.Conn, opts ...func(*Conn)) *Conn {
// For v1 the header length is at most 108 bytes.
// For v2 the header length is at most 52 bytes plus the length of the TLVs.
// We use 256 bytes to be safe.
const bufSize = 256
br := bufio.NewReaderSize(conn, bufSize)

pConn := &Conn{
bufReader: bufio.NewReader(conn),
bufReader: br,
reader: io.MultiReader(br, conn),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the idea but this does not look right: io.MultiReader will read br reader until EOF but it is a buffered reader that will read until connection returns EOF.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be something like #119

conn: conn,
}

Expand All @@ -157,7 +165,7 @@ func (p *Conn) Read(b []byte) (int, error) {
return 0, p.readErr
}

return p.bufReader.Read(b)
return p.reader.Read(b)
}

// Write wraps original conn.Write
Expand Down Expand Up @@ -339,5 +347,27 @@ func (p *Conn) WriteTo(w io.Writer) (int64, error) {
if p.readErr != nil {
return 0, p.readErr
}
return p.bufReader.WriteTo(w)

b := make([]byte, p.bufReader.Buffered())
if _, err := p.bufReader.Read(b); err != nil {
return 0, err // this should never as we read buffered data
}

var n int64
{
nn, err := w.Write(b)
n += int64(nn)
if err != nil {
return n, err
}
}
{
nn, err := io.Copy(w, p.conn)
n += nn
if err != nil {
return n, err
}
}

return n, nil
}
5 changes: 2 additions & 3 deletions protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"testing"
"time"
Expand Down Expand Up @@ -1480,7 +1479,7 @@ type testConn struct {

func (c *testConn) ReadFrom(r io.Reader) (int64, error) {
c.readFromCalledWith = r
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
return int64(len(b)), err
}
func (c *testConn) Write(p []byte) (int, error) {
Expand Down Expand Up @@ -1625,7 +1624,7 @@ func benchmarkTCPProxy(size int, b *testing.B) {

}()
//receive data
n, err := io.Copy(ioutil.Discard, conn)
n, err := io.Copy(io.Discard, conn)
if n != int64(len(data)) {
b.Fatalf("Expected to receive %d bytes, got %d", len(data), n)
}
Expand Down
Loading