diff --git a/client/client.go b/client/client.go index d09e70f..5ad046e 100644 --- a/client/client.go +++ b/client/client.go @@ -48,6 +48,7 @@ type Client struct { jobhandlers map[string]JobHandler + isConn bool conn net.Conn addr string mutex sync.RWMutex @@ -73,6 +74,7 @@ func New(addr string) (client *Client, err error) { if err = client.connect(); err != nil { return } + client.isConn = true go client.inLoop() go client.outLoop() return @@ -105,6 +107,9 @@ func (client *Client) readData(length int) (data []byte, err error) { // read until data can be unpacked for i := length; i > 0 || len(data) < common.PACKET_LEN; i -= n { if n, err = client.conn.Read(buf); err != nil { + if !client.isConn { + return nil, common.ErrConnClosed + } if err == io.EOF && n == 0 { if data == nil { err = common.ErrConnection @@ -193,7 +198,9 @@ func (client *Client) inLoop() { if err == common.ErrConnection { client.Close() } - client.err(err) + if err != common.ErrConnClosed { + client.err(err) + } break } job, err := decodeJob(rel) @@ -358,6 +365,7 @@ func (client *Client) Echo(data []byte) (r []byte) { // Close func (client *Client) Close() (err error) { + client.isConn = false close(client.in) close(client.out) diff --git a/common/error.go b/common/error.go index 4ee3195..fa52530 100644 --- a/common/error.go +++ b/common/error.go @@ -13,19 +13,20 @@ import ( ) var ( - ErrJobTimeOut = errors.New("Do a job time out.") - ErrInvalidData = errors.New("Invalid data.") - ErrWorkWarning = errors.New("Work warning.") - ErrWorkFail = errors.New("Work fail.") - ErrWorkException = errors.New("Work exeption.") - ErrDataType = errors.New("Invalid data type.") - ErrOutOfCap = errors.New("Out of the capability.") - ErrNotConn = errors.New("Did not connect to job server.") - ErrFuncNotFound = errors.New("The function was not found.") - ErrConnection = errors.New("Connection error.") - ErrNoActiveAgent = errors.New("No active agent.") - ErrTimeOut = errors.New("Executing time out.") - ErrUnknown = errors.New("Unknown error.") + ErrJobTimeOut = errors.New("Do a job time out") + ErrInvalidData = errors.New("Invalid data") + ErrWorkWarning = errors.New("Work warning") + ErrWorkFail = errors.New("Work fail") + ErrWorkException = errors.New("Work exeption") + ErrDataType = errors.New("Invalid data type") + ErrOutOfCap = errors.New("Out of the capability") + ErrNotConn = errors.New("Did not connect to job server") + ErrFuncNotFound = errors.New("The function was not found") + ErrConnection = errors.New("Connection error") + ErrNoActiveAgent = errors.New("No active agent") + ErrTimeOut = errors.New("Executing time out") + ErrUnknown = errors.New("Unknown error") + ErrConnClosed = errors.New("Connection closed") ) func DisablePanic() {recover()} diff --git a/gearman_test.go b/gearman_test.go index c9c774b..9e35b1c 100644 --- a/gearman_test.go +++ b/gearman_test.go @@ -63,7 +63,8 @@ func TestJobs(t *testing.T) { defer c.Close() c.ErrHandler = func(e error) { - t.Error(e) +// t.Error(e) + t.Log(e) } {