Skip to content

Commit 1994c14

Browse files
committed
Implement io.ReaderFrom/WriterTo for Conn
This change increase performance when proxying wrapped connections using io.Copy. Since go 1.11 copying between tcp connections uses the splice system call on linux yielding considerable performance improvments. See: https://golang.org/doc/go1.11#net Signed-off-by: Fabian Ruff <[email protected]>
1 parent fff0abf commit 1994c14

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

protocol.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package proxyproto
22

33
import (
44
"bufio"
5+
"io"
56
"net"
67
"sync"
78
"time"
@@ -237,3 +238,20 @@ func (p *Conn) readHeader() error {
237238

238239
return err
239240
}
241+
242+
// ReadFrom implements the io.ReaderFrom ReadFrom method
243+
func (p *Conn) ReadFrom(r io.Reader) (int64, error) {
244+
if rf, ok := p.conn.(io.ReaderFrom); ok {
245+
return rf.ReadFrom(r)
246+
}
247+
return io.Copy(p.conn, r)
248+
}
249+
250+
// WriteTo implements io.WriterTo
251+
func (p *Conn) WriteTo(w io.Writer) (int64, error) {
252+
p.once.Do(func() { p.readErr = p.readHeader() })
253+
if p.readErr != nil {
254+
return 0, p.readErr
255+
}
256+
return p.bufReader.WriteTo(w)
257+
}

0 commit comments

Comments
 (0)