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.
This commit is contained in:
Miek Gieben 2012-11-22 15:06:21 +01:00
parent 6e605f578e
commit 1297852fdc
1 changed files with 17 additions and 12 deletions

View File

@ -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