fixed #31
This commit is contained in:
parent
0a6bacbe82
commit
45a9d7c3e5
@ -18,7 +18,6 @@ type Client struct {
|
|||||||
respHandler map[string]ResponseHandler
|
respHandler map[string]ResponseHandler
|
||||||
innerHandler map[string]ResponseHandler
|
innerHandler map[string]ResponseHandler
|
||||||
in chan *Response
|
in chan *Response
|
||||||
isConn bool
|
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
rw *bufio.ReadWriter
|
rw *bufio.ReadWriter
|
||||||
|
|
||||||
@ -40,7 +39,6 @@ func New(network, addr string) (client *Client, err error) {
|
|||||||
}
|
}
|
||||||
client.rw = bufio.NewReadWriter(bufio.NewReader(client.conn),
|
client.rw = bufio.NewReadWriter(bufio.NewReader(client.conn),
|
||||||
bufio.NewWriter(client.conn))
|
bufio.NewWriter(client.conn))
|
||||||
client.isConn = true
|
|
||||||
go client.readLoop()
|
go client.readLoop()
|
||||||
go client.processLoop()
|
go client.processLoop()
|
||||||
return
|
return
|
||||||
@ -83,7 +81,7 @@ func (client *Client) readLoop() {
|
|||||||
var err error
|
var err error
|
||||||
var resp *Response
|
var resp *Response
|
||||||
ReadLoop:
|
ReadLoop:
|
||||||
for {
|
for client.conn != nil {
|
||||||
if data, err = client.read(bufferSize); err != nil {
|
if data, err = client.read(bufferSize); err != nil {
|
||||||
client.err(err)
|
client.err(err)
|
||||||
if err == ErrLostConn {
|
if err == ErrLostConn {
|
||||||
@ -178,6 +176,9 @@ func (client *Client) handleInner(key string, resp *Response) *Response {
|
|||||||
|
|
||||||
func (client *Client) do(funcname string, data []byte,
|
func (client *Client) do(funcname string, data []byte,
|
||||||
flag uint32) (handle string, err error) {
|
flag uint32) (handle string, err error) {
|
||||||
|
if client.conn == nil {
|
||||||
|
return "", ErrLostConn
|
||||||
|
}
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
client.lastcall = "c"
|
client.lastcall = "c"
|
||||||
@ -211,7 +212,7 @@ func (client *Client) Do(funcname string, data []byte,
|
|||||||
datatype = dtSubmitJob
|
datatype = dtSubmitJob
|
||||||
}
|
}
|
||||||
handle, err = client.do(funcname, data, datatype)
|
handle, err = client.do(funcname, data, datatype)
|
||||||
if h != nil {
|
if err == nil && h != nil {
|
||||||
client.respHandler[handle] = h
|
client.respHandler[handle] = h
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -221,6 +222,9 @@ func (client *Client) Do(funcname string, data []byte,
|
|||||||
// flag can be set to: JobLow, JobNormal and JobHigh
|
// flag can be set to: JobLow, JobNormal and JobHigh
|
||||||
func (client *Client) DoBg(funcname string, data []byte,
|
func (client *Client) DoBg(funcname string, data []byte,
|
||||||
flag byte) (handle string, err error) {
|
flag byte) (handle string, err error) {
|
||||||
|
if client.conn == nil {
|
||||||
|
return "", ErrLostConn
|
||||||
|
}
|
||||||
var datatype uint32
|
var datatype uint32
|
||||||
switch flag {
|
switch flag {
|
||||||
case JobLow:
|
case JobLow:
|
||||||
@ -236,6 +240,9 @@ func (client *Client) DoBg(funcname string, data []byte,
|
|||||||
|
|
||||||
// Get job status from job server.
|
// Get job status from job server.
|
||||||
func (client *Client) Status(handle string) (status *Status, err error) {
|
func (client *Client) Status(handle string) (status *Status, err error) {
|
||||||
|
if client.conn == nil {
|
||||||
|
return nil, ErrLostConn
|
||||||
|
}
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
client.lastcall = "s" + handle
|
client.lastcall = "s" + handle
|
||||||
@ -257,6 +264,9 @@ func (client *Client) Status(handle string) (status *Status, err error) {
|
|||||||
|
|
||||||
// Echo.
|
// Echo.
|
||||||
func (client *Client) Echo(data []byte) (echo []byte, err error) {
|
func (client *Client) Echo(data []byte) (echo []byte, err error) {
|
||||||
|
if client.conn == nil {
|
||||||
|
return nil, ErrLostConn
|
||||||
|
}
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
client.innerHandler["e"] = func(resp *Response) {
|
client.innerHandler["e"] = func(resp *Response) {
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"github.com/mikespook/gearman-go/client"
|
"github.com/mikespook/gearman-go/client"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -19,6 +20,7 @@ func main() {
|
|||||||
defer c.Close()
|
defer c.Close()
|
||||||
c.ErrorHandler = func(e error) {
|
c.ErrorHandler = func(e error) {
|
||||||
log.Println(e)
|
log.Println(e)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
echo := []byte("Hello\x00 world")
|
echo := []byte("Hello\x00 world")
|
||||||
echomsg, err := c.Echo(echo)
|
echomsg, err := c.Echo(echo)
|
||||||
|
Loading…
Reference in New Issue
Block a user