From 45a9d7c3e5073202e177285213406d09a8a3e824 Mon Sep 17 00:00:00 2001 From: Xing Xing Date: Mon, 3 Mar 2014 11:40:42 +0800 Subject: [PATCH] fixed #31 --- client/client.go | 18 ++++++++++++++---- example/client/client.go | 2 ++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/client/client.go b/client/client.go index e7e6444..52e401d 100644 --- a/client/client.go +++ b/client/client.go @@ -18,7 +18,6 @@ type Client struct { respHandler map[string]ResponseHandler innerHandler map[string]ResponseHandler in chan *Response - isConn bool conn net.Conn rw *bufio.ReadWriter @@ -40,7 +39,6 @@ func New(network, addr string) (client *Client, err error) { } client.rw = bufio.NewReadWriter(bufio.NewReader(client.conn), bufio.NewWriter(client.conn)) - client.isConn = true go client.readLoop() go client.processLoop() return @@ -83,7 +81,7 @@ func (client *Client) readLoop() { var err error var resp *Response ReadLoop: - for { + for client.conn != nil { if data, err = client.read(bufferSize); err != nil { client.err(err) if err == ErrLostConn { @@ -178,6 +176,9 @@ func (client *Client) handleInner(key string, resp *Response) *Response { func (client *Client) do(funcname string, data []byte, flag uint32) (handle string, err error) { + if client.conn == nil { + return "", ErrLostConn + } var mutex sync.Mutex mutex.Lock() client.lastcall = "c" @@ -211,7 +212,7 @@ func (client *Client) Do(funcname string, data []byte, datatype = dtSubmitJob } handle, err = client.do(funcname, data, datatype) - if h != nil { + if err == nil && h != nil { client.respHandler[handle] = h } return @@ -221,6 +222,9 @@ func (client *Client) Do(funcname string, data []byte, // flag can be set to: JobLow, JobNormal and JobHigh func (client *Client) DoBg(funcname string, data []byte, flag byte) (handle string, err error) { + if client.conn == nil { + return "", ErrLostConn + } var datatype uint32 switch flag { case JobLow: @@ -236,6 +240,9 @@ func (client *Client) DoBg(funcname string, data []byte, // Get job status from job server. func (client *Client) Status(handle string) (status *Status, err error) { + if client.conn == nil { + return nil, ErrLostConn + } var mutex sync.Mutex mutex.Lock() client.lastcall = "s" + handle @@ -257,6 +264,9 @@ func (client *Client) Status(handle string) (status *Status, err error) { // Echo. func (client *Client) Echo(data []byte) (echo []byte, err error) { + if client.conn == nil { + return nil, ErrLostConn + } var mutex sync.Mutex mutex.Lock() client.innerHandler["e"] = func(resp *Response) { diff --git a/example/client/client.go b/example/client/client.go index e8dc4f6..06efcb2 100644 --- a/example/client/client.go +++ b/example/client/client.go @@ -4,6 +4,7 @@ import ( "github.com/mikespook/gearman-go/client" "log" "sync" + "os" ) func main() { @@ -19,6 +20,7 @@ func main() { defer c.Close() c.ErrorHandler = func(e error) { log.Println(e) + os.Exit(1) } echo := []byte("Hello\x00 world") echomsg, err := c.Echo(echo)