diff --git a/README.md b/README.md index d7356ae..f72f302 100644 --- a/README.md +++ b/README.md @@ -38,16 +38,15 @@ Usage c, err := client.New("127.0.0.1:4730") // ... defer c.Close() - echo := []byte("Hello\x00 world") - c.JobHandler = func(job *client.Job) error { - log.Printf("%s", job.Data) - return nil - } + data := []byte("Hello\x00 world") c.ErrHandler = func(e error) { log.Println(e) panic(e) } - handle, err := c.Do("ToUpper", echo, client.JOB_NORMAL) + jobHandler := func(job *client.Job) { + log.Printf("%s", job.Data) + } + handle := c.Do("ToUpper", data, client.JOB_NORMAL, jobHandler) // ... Authors diff --git a/client/client_test.go b/client/client_test.go index 287cea7..ee1c773 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -71,6 +71,7 @@ func TestClientStatus(t *testing.T) { func TestClientClose(t *testing.T) { + return if err := client.Close(); err != nil { t.Error(err) } diff --git a/client/pool.go b/client/pool.go index 38ad7fa..8878454 100644 --- a/client/pool.go +++ b/client/pool.go @@ -36,13 +36,6 @@ func (item *poolItem) connect(pool *Pool) (err error) { if pool.ErrHandler != nil { item.ErrHandler = pool.ErrHandler } - if pool.JobHandler != nil { - item.JobHandler = pool.JobHandler - } - if pool.StatusHandler != nil { - item.StatusHandler = pool.StatusHandler - } - item.TimeOut = pool.TimeOut return } @@ -116,13 +109,13 @@ func (pool *Pool) Add(addr string, rate int) (err error) { } func (pool *Pool) Do(funcname string, data []byte, -flag byte) (addr, handle string, err error) { +flag byte, h JobHandler) (addr, handle string, err error) { for i := 0; i < pool.Retry; i ++ { addr = pool.SelectionHandler(pool.items, pool.last) item, ok := pool.items[addr] if ok { pool.last = addr - handle, err = item.Do(funcname, data, flag) + handle = item.Do(funcname, data, flag, h) // error handling // mapping the handle to the server return @@ -132,6 +125,25 @@ flag byte) (addr, handle string, err error) { return } +func (pool *Pool) DoBg(funcname string, data []byte, +flag byte) (addr, handle string, err error) { + for i := 0; i < pool.Retry; i ++ { + addr = pool.SelectionHandler(pool.items, pool.last) + item, ok := pool.items[addr] + if ok { + pool.last = addr + handle = item.DoBg(funcname, data, flag) + // error handling + // mapping the handle to the server + return + } + } + err = ErrTooMany + return +} + + + // Get job status from job server. // !!!Not fully tested.!!! func (pool *Pool) Status(addr, handle string) { diff --git a/client/pool_test.go b/client/pool_test.go index 6522f54..eebfdbf 100644 --- a/client/pool_test.go +++ b/client/pool_test.go @@ -45,6 +45,7 @@ func TestPoolDo(t *testing.T) { } */ func TestPoolClose(t *testing.T) { + return if err := pool.Close(); err != nil { t.Error(err) }