From f880354a61fb636fdf5f11be85ff03adaf7f6005 Mon Sep 17 00:00:00 2001 From: Xing Xing Date: Mon, 3 Mar 2014 14:45:35 +0800 Subject: [PATCH] type assertion with *net.OpError --- client/client.go | 14 ++++++++------ worker/agent.go | 23 +++++++---------------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/client/client.go b/client/client.go index 52e401d..5e12dbb 100644 --- a/client/client.go +++ b/client/client.go @@ -4,7 +4,6 @@ package client import ( "bufio" - "io" "net" "sync" ) @@ -62,9 +61,6 @@ func (client *Client) read(length int) (data []byte, err error) { // read until data can be unpacked for i := length; i > 0 || len(data) < minPacketLength; i -= n { if n, err = client.rw.Read(buf); err != nil { - if err == io.EOF { - err = ErrLostConn - } return } data = append(data, buf[0:n]...) @@ -83,10 +79,16 @@ func (client *Client) readLoop() { ReadLoop: for client.conn != nil { if data, err = client.read(bufferSize); err != nil { - client.err(err) - if err == ErrLostConn { + if opErr, ok := err.(*net.OpError); ok { + if opErr.Timeout() { + client.err(err) + } + if opErr.Temporary() { + continue + } break } + client.err(err) // If it is unexpected error and the connection wasn't // closed by Gearmand, the client should close the conection // and reconnect to job server. diff --git a/worker/agent.go b/worker/agent.go index 3ac7f3b..2236173 100644 --- a/worker/agent.go +++ b/worker/agent.go @@ -2,9 +2,7 @@ package worker import ( "bufio" - "io" "net" - "strings" "sync" ) @@ -54,7 +52,13 @@ func (a *agent) work() { var data, leftdata []byte for { if data, err = a.read(bufferSize); err != nil { - if err == ErrLostConn { + if opErr, ok := err.(*net.OpError); ok { + if opErr.Timeout() { + a.worker.err(err) + } + if opErr.Temporary() { + continue + } break } a.worker.err(err) @@ -116,16 +120,6 @@ func (a *agent) PreSleep() { a.write(outpack) } -func isClosed(err error) bool { - switch { - case err == io.EOF: - fallthrough - case strings.Contains(err.Error(), "use of closed network connection"): - return true - } - return false -} - // read length bytes from the socket func (a *agent) read(length int) (data []byte, err error) { n := 0 @@ -133,9 +127,6 @@ func (a *agent) read(length int) (data []byte, err error) { // read until data can be unpacked for i := length; i > 0 || len(data) < minPacketLength; i -= n { if n, err = a.rw.Read(buf); err != nil { - if isClosed(err) { - err = ErrLostConn - } return } data = append(data, buf[0:n]...)