From 1297852fdcc45eb13114a9e006e6e7a0575c2604 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Thu, 22 Nov 2012 15:06:21 +0100 Subject: [PATCH] Fix tcp read/writes and client.Attempts Thanks to a bug report from Vitalie Cherpec which commented that this didn't work. The timeout settings we not in the client.attempts loop, so they were only active for the first attempt. And the loop wasn't broken out of when the write or read succeeded. Both these issues are now fixed. --- client.go | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index 587f6a44..8bfecb40 100644 --- a/client.go +++ b/client.go @@ -70,13 +70,12 @@ func (w *reply) dial() (err error) { } for a := 0; a < attempts; a++ { if w.client.Net == "" { - conn, err = net.Dial("udp", w.addr) + conn, err = net.DialTimeout("udp", w.addr, 5 * 1e9) } else { - conn, err = net.Dial(w.client.Net, w.addr) + conn, err = net.DialTimeout(w.client.Net, w.addr, 5 * 1e9) } - if err != nil { - // There are no timeouts defined? - continue + if err == nil { + break } } w.conn = conn @@ -128,8 +127,8 @@ func (w *reply) read(p []byte) (n int, err error) { } switch w.client.Net { case "tcp", "tcp4", "tcp6": - setTimeouts(w) for a := 0; a < attempts; a++ { + setTimeouts(w) n, err = w.conn.(*net.TCPConn).Read(p[0:2]) if err != nil || n != 2 { if e, ok := err.(net.Error); ok && e.Timeout() { @@ -164,20 +163,23 @@ func (w *reply) read(p []byte) (n int, err error) { i += j } n = i + if err == nil { + return n, err + } } case "", "udp", "udp4", "udp6": for a := 0; a < attempts; a++ { setTimeouts(w) n, _, err = w.conn.(*net.UDPConn).ReadFromUDP(p) - if err == nil { - return n, err - } if err != nil { if e, ok := err.(net.Error); ok && e.Timeout() { continue } return n, err } + if err == nil { + return n, err + } } } return @@ -244,20 +246,23 @@ func (w *reply) write(p []byte) (n int, err error) { i += j } n = i + if err == nil { + return n, err + } } case "", "udp", "udp4", "udp6": for a := 0; a < attempts; a++ { setTimeouts(w) n, err = w.conn.(*net.UDPConn).Write(p) - if err == nil { - return - } if err != nil { if e, ok := err.(net.Error); ok && e.Timeout() { continue } return n, err } + if err == nil { + return n, err + } } } return