fix: increase UDP read buffer length to max datagram size
Co-authored-by: Tom Moulard <tom.moulard@traefik.io>
This commit is contained in:
parent
9df053e3f5
commit
db4a92d877
4 changed files with 137 additions and 18 deletions
|
@ -20,14 +20,14 @@ func NewProxy(address string) (*Proxy, error) {
|
|||
|
||||
// ServeUDP implements the Handler interface.
|
||||
func (p *Proxy) ServeUDP(conn *Conn) {
|
||||
log.Debugf("Handling connection from %s", conn.rAddr)
|
||||
log.WithoutContext().Debugf("Handling connection from %s", conn.rAddr)
|
||||
|
||||
// needed because of e.g. server.trackedConnection
|
||||
defer conn.Close()
|
||||
|
||||
connBackend, err := net.Dial("udp", p.target)
|
||||
if err != nil {
|
||||
log.Errorf("Error while connecting to backend: %v", err)
|
||||
log.WithoutContext().Errorf("Error while connecting to backend: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -35,8 +35,8 @@ func (p *Proxy) ServeUDP(conn *Conn) {
|
|||
defer connBackend.Close()
|
||||
|
||||
errChan := make(chan error)
|
||||
go p.connCopy(conn, connBackend, errChan)
|
||||
go p.connCopy(connBackend, conn, errChan)
|
||||
go connCopy(conn, connBackend, errChan)
|
||||
go connCopy(connBackend, conn, errChan)
|
||||
|
||||
err = <-errChan
|
||||
if err != nil {
|
||||
|
@ -46,8 +46,12 @@ func (p *Proxy) ServeUDP(conn *Conn) {
|
|||
<-errChan
|
||||
}
|
||||
|
||||
func (p Proxy) connCopy(dst io.WriteCloser, src io.Reader, errCh chan error) {
|
||||
_, err := io.Copy(dst, src)
|
||||
func connCopy(dst io.WriteCloser, src io.Reader, errCh chan error) {
|
||||
// The buffer is initialized to the maximum UDP datagram size,
|
||||
// to make sure that the whole UDP datagram is read or written atomically (no data is discarded).
|
||||
buffer := make([]byte, maxDatagramSize)
|
||||
|
||||
_, err := io.CopyBuffer(dst, src, buffer)
|
||||
errCh <- err
|
||||
|
||||
if err := dst.Close(); err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue