race is bad, how should we do?

This commit is contained in:
Xing Xing 2013-09-22 22:58:22 +08:00
parent e3f789bc86
commit c8f2f5085c
3 changed files with 49 additions and 47 deletions

View File

@ -9,6 +9,7 @@ import (
"io" "io"
"net" "net"
"sync" "sync"
"fmt"
) )
/* /*
@ -114,6 +115,7 @@ func (client *Client) readLoop() {
client.err(err) client.err(err)
continue continue
} }
fmt.Printf("[%X]", data)
client.in <- data client.in <- data
} }
close(client.in) close(client.in)
@ -191,10 +193,6 @@ func (client *Client) handleInner(key string, resp *Response) {
// Internal do // Internal do
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) {
id := IdGen.Id()
req := getJob(id, []byte(funcname), data)
req.DataType = flag
client.write(req)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
client.mutex.RLock() client.mutex.RLock()
@ -208,6 +206,10 @@ func (client *Client) do(funcname string, data []byte,
} }
handle = resp.Handle handle = resp.Handle
}) })
id := IdGen.Id()
req := getJob(id, []byte(funcname), data)
req.DataType = flag
client.write(req)
wg.Wait() wg.Wait()
return return
} }
@ -260,10 +262,6 @@ func (client *Client) DoBg(funcname string, data []byte,
// Get job status from job server. // Get job status from job server.
// !!!Not fully tested.!!! // !!!Not fully tested.!!!
func (client *Client) Status(handle string) (status *Status, err error) { func (client *Client) Status(handle string) (status *Status, err error) {
req := getRequest()
req.DataType = GET_STATUS
req.Data = []byte(handle)
client.write(req)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
client.mutex.Lock() client.mutex.Lock()
@ -277,25 +275,27 @@ func (client *Client) Status(handle string) (status *Status, err error) {
client.err(err) client.err(err)
} }
}) })
req := getRequest()
req.DataType = GET_STATUS
req.Data = []byte(handle)
client.write(req)
wg.Wait() wg.Wait()
return return
} }
// Send a something out, get the samething back. // Send a something out, get the samething back.
func (client *Client) Echo(data []byte) (echo []byte, err error) { func (client *Client) Echo(data []byte) (echo []byte, err error) {
var wg sync.WaitGroup
wg.Add(1)
client.innerHandler["e"] = ResponseHandler(func(resp *Response) {
defer wg.Done()
echo = resp.Data
})
req := getRequest() req := getRequest()
req.DataType = ECHO_REQ req.DataType = ECHO_REQ
req.Data = data req.Data = data
client.write(req) client.write(req)
var wg sync.WaitGroup
wg.Add(1)
client.mutex.Lock()
client.lastcall = "e" client.lastcall = "e"
client.innerHandler["e"] = ResponseHandler(func(resp *Response) {
defer wg.Done()
defer client.mutex.Unlock()
echo = resp.Data
})
wg.Wait() wg.Wait()
return return
} }

View File

@ -22,30 +22,30 @@ const (
RES_STR = "\x00RES" RES_STR = "\x00RES"
// package data type // package data type
CAN_DO = 1 CAN_DO = 0x1
CANT_DO = 2 CANT_DO = 0x2
RESET_ABILITIES = 3 RESET_ABILITIES = 0x3
PRE_SLEEP = 4 PRE_SLEEP = 0x4
NOOP = 6 NOOP = 0x6
JOB_CREATED = 8 JOB_CREATED = 0x8
GRAB_JOB = 9 GRAB_JOB = 0x9
NO_JOB = 10 NO_JOB = 0xa
JOB_ASSIGN = 11 JOB_ASSIGN = 0xb
WORK_STATUS = 12 WORK_STATUS = 0xc
WORK_COMPLETE = 13 WORK_COMPLETE = 0xd
WORK_FAIL = 14 WORK_FAIL = 0xe
GET_STATUS = 15 GET_STATUS = 0xf
ECHO_REQ = 16 ECHO_REQ = 0x10
ECHO_RES = 17 ECHO_RES = 0x11
ERROR = 19 ERROR = 0x13
STATUS_RES = 20 STATUS_RES = 0x14
SET_CLIENT_ID = 22 SET_CLIENT_ID = 0x16
CAN_DO_TIMEOUT = 23 CAN_DO_TIMEOUT = 0x17
WORK_EXCEPTION = 25 WORK_EXCEPTION = 0x19
WORK_DATA = 28 WORK_DATA = 0x1c
WORK_WARNING = 29 WORK_WARNING = 0x1d
GRAB_JOB_UNIQ = 30 GRAB_JOB_UNIQ = 0x1e
JOB_ASSIGN_UNIQ = 31 JOB_ASSIGN_UNIQ = 0x1f
SUBMIT_JOB = 7 SUBMIT_JOB = 7
SUBMIT_JOB_BG = 18 SUBMIT_JOB_BG = 18

View File

@ -3,7 +3,6 @@ package main
import ( import (
"log" "log"
"sync" "sync"
"time"
"github.com/mikespook/gearman-go/client" "github.com/mikespook/gearman-go/client"
) )
@ -14,29 +13,32 @@ func main() {
// by implementing IdGenerator interface. // by implementing IdGenerator interface.
// client.IdGen = client.NewAutoIncId() // client.IdGen = client.NewAutoIncId()
c, err := client.New("127.0.0.1:4730") c, err := client.New("tcp4", "127.0.0.1:4730")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer c.Close() defer c.Close()
c.ErrHandler = func(e error) { c.ErrorHandler = func(e error) {
log.Println(e) log.Println(e)
} }
echo := []byte("Hello\x00 world") echo := []byte("Hello\x00 world")
wg.Add(1) wg.Add(1)
echomsg, err := c.Echo(echo, time.Second) echomsg, err := c.Echo(echo)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
log.Println(string(echomsg)) log.Println(string(echomsg))
wg.Done() wg.Done()
jobHandler := func(job *client.Job) { jobHandler := func(job *client.Response) {
log.Printf("%s", job.Data) log.Printf("%s", job.Data)
wg.Done() wg.Done()
} }
handle := c.Do("ToUpper", echo, client.JOB_NORMAL, jobHandler) handle, err := c.Do("ToUpper", echo, client.JOB_NORMAL, jobHandler)
if err != nil {
log.Fatalln(err)
}
wg.Add(1) wg.Add(1)
status, err := c.Status(handle, time.Second) status, err := c.Status(handle)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }