gearman-go/client/client.go

259 lines
5.4 KiB
Go
Raw Normal View History

// Copyright 2011 Xing Xing <mikespook@gmail.com>.
// All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package client
import (
2013-08-29 16:51:23 +08:00
"io"
"net"
"sync"
"github.com/mikespook/golib/idgen"
)
2013-08-29 16:51:23 +08:00
/*
The client side api for gearman
usage:
c := client.New("tcp4", "127.0.0.1:4730")
handle := c.Do("foobar", []byte("data here"), JOB_LOW | JOB_BG)
*/
type Client struct {
2013-08-29 16:51:23 +08:00
net, addr string
respHandler map[string]ResponseHandler
createdHandler ResponseHandler
in chan []byte
isConn bool
conn net.Conn
mutex sync.RWMutex
ErrorHandler ErrorHandler
IdGen idgen.IdGen
}
// Create a new client.
// Connect to "addr" through "network"
// Eg.
// client, err := client.New("127.0.0.1:4730")
2013-08-29 16:51:23 +08:00
func New(net, addr string) (client *Client, err error) {
client = &Client{
net: net,
addr: addr,
respHandler: make(map[string]ResponseHandler, QUEUE_SIZE),
in: make(chan []byte, QUEUE_SIZE),
}
if err = client.connect(); err != nil {
return
}
client.isConn = true
go client.readLoop()
go client.processLoop()
return
}
2013-01-14 17:59:48 +08:00
// {{{ private functions
2013-08-29 16:51:23 +08:00
//
2013-01-14 17:59:48 +08:00
func (client *Client) connect() (err error) {
2013-08-29 16:51:23 +08:00
client.conn, err = net.Dial(client.net, client.addr)
return
2013-01-14 17:59:48 +08:00
}
// Internal write
2013-08-29 16:51:23 +08:00
func (client *Client) write(req *request) (err error) {
var n int
buf := req.Encode()
for i := 0; i < len(buf); i += n {
n, err = client.conn.Write(buf[i:])
if err != nil {
return
}
}
return
2013-01-14 17:59:48 +08:00
}
// read length bytes from the socket
2013-08-29 16:51:23 +08:00
func (client *Client) read(length int) (data []byte, err error) {
n := 0
buf := getBuffer(BUFFER_SIZE)
// read until data can be unpacked
for i := length; i > 0 || len(data) < MIN_PACKET_LEN; i -= n {
if n, err = client.conn.Read(buf); err != nil {
if !client.isConn {
err = ErrConnClosed
return
}
if err == io.EOF && n == 0 {
if data == nil {
err = ErrConnection
}
}
return
}
data = append(data, buf[0:n]...)
if n < BUFFER_SIZE {
break
}
}
return
2013-01-14 17:59:48 +08:00
}
2013-08-29 16:51:23 +08:00
// read data from socket
func (client *Client) readLoop() {
var data []byte
var err error
for client.isConn {
if data, err = client.read(BUFFER_SIZE); err != nil {
if err == ErrConnClosed {
break
}
client.err(err)
continue
}
client.in <- data
}
close(client.in)
2013-01-14 17:59:48 +08:00
}
2013-08-29 16:51:23 +08:00
// decode data & process it
func (client *Client) processLoop() {
var resp *response
var l int
var err error
var data, leftdata []byte
for data = range client.in {
l = len(data)
if len(leftdata) > 0 { // some data left for processing
data = append(leftdata, data ...)
}
if l < MIN_PACKET_LEN { // not enough data
leftdata = data
continue
}
if resp, l, err = decodeResponse(data); err != nil {
client.err(err)
continue
}
switch resp.DataType {
case WORK_DATA, WORK_WARNING, WORK_STATUS, WORK_COMPLETE,
WORK_FAIL, WORK_EXCEPTION:
client.handleResponse(string(resp.Handle), resp)
}
if len(data) > l {
leftdata = data[l:]
}
}
}
// error handler
2013-08-29 16:51:23 +08:00
func (client *Client) err(e error) {
if client.ErrorHandler != nil {
client.ErrorHandler(e)
}
}
// job handler
2013-08-29 16:51:23 +08:00
func (client *Client) handleResponse(key string, resp *response) {
client.mutex.RLock()
defer client.mutex.RUnlock()
if h, ok := client.respHandler[key]; ok {
h(resp)
delete(client.respHandler, string(resp.Handle))
}
2013-01-14 17:59:48 +08:00
}
// Internal do
2013-01-15 17:55:44 +08:00
func (client *Client) do(funcname string, data []byte,
2013-08-29 16:51:23 +08:00
flag uint32) (handle []byte) {
req := getJob(funcname, client.IdGen.Id().(string), data)
client.mutex.Lock()
defer client.mutex.Unlock()
client.write(req)
var wg sync.WaitGroup
wg.Add(1)
client.createdHandler = func(resp *response) {
defer wg.Done()
handle = resp.Handle
}
wg.Wait()
return
2013-01-14 17:59:48 +08:00
}
// }}}
// Do the function.
// funcname is a string with function name.
// data is encoding to byte array.
2013-08-29 16:51:23 +08:00
// flag set the job type, include running level: JOB_LOW, JOB_NORMAL, JOB_HIGH
2013-01-15 17:55:44 +08:00
func (client *Client) Do(funcname string, data []byte,
2013-08-29 16:51:23 +08:00
flag byte, h ResponseHandler) (handle []byte) {
var datatype uint32
switch flag {
case JOB_LOW:
datatype = SUBMIT_JOB_LOW
case JOB_HIGH:
datatype = SUBMIT_JOB_HIGH
default:
datatype = SUBMIT_JOB
}
handle = client.do(funcname, data, datatype)
client.mutex.Lock()
defer client.mutex.Unlock()
if h != nil {
client.respHandler[string(handle)] = h
}
return
2013-01-14 17:59:48 +08:00
}
2013-08-29 16:51:23 +08:00
// Do the function at background.
// funcname is a string with function name.
// data is encoding to byte array.
// flag set the job type, include running level: JOB_LOW, JOB_NORMAL, JOB_HIGH
2013-01-15 17:55:44 +08:00
func (client *Client) DoBg(funcname string, data []byte,
2013-08-29 16:51:23 +08:00
flag byte) (handle []byte) {
var datatype uint32
switch flag {
case JOB_LOW:
datatype = SUBMIT_JOB_LOW_BG
case JOB_HIGH:
datatype = SUBMIT_JOB_HIGH_BG
default:
datatype = SUBMIT_JOB_BG
}
handle = client.do(funcname, data, datatype)
return
}
// Get job status from job server.
// !!!Not fully tested.!!!
2013-08-29 16:51:23 +08:00
func (client *Client) Status(handle []byte, h ResponseHandler) (err error) {
req := getRequest()
req.DataType = GET_STATUS
req.Data = handle
client.write(req)
if h != nil {
client.respHandler["status-" + string(handle)] = h
}
return
}
// Send a something out, get the samething back.
2013-08-29 16:51:23 +08:00
func (client *Client) Echo(data []byte, h ResponseHandler) (err error) {
req := getRequest()
req.DataType = ECHO_REQ
req.Data = data
client.write(req)
if h != nil {
client.respHandler["echo"] = h
}
return
}
// Close
func (client *Client) Close() (err error) {
2013-08-29 16:51:23 +08:00
client.isConn = false
return client.conn.Close()
}