diff --git a/client/client.go b/client/client.go index cf4da57..8fde1dc 100644 --- a/client/client.go +++ b/client/client.go @@ -9,7 +9,6 @@ import ( "io" "net" "sync" - "github.com/mikespook/golib/idgen" ) /* @@ -30,7 +29,6 @@ type Client struct { mutex sync.RWMutex ErrorHandler ErrorHandler - IdGen idgen.IdGen } // Create a new client. @@ -44,7 +42,6 @@ func New(net, addr string) (client *Client, err error) { respHandler: make(map[string]ResponseHandler, QUEUE_SIZE), innerHandler: make(map[string]ResponseHandler, QUEUE_SIZE), in: make(chan []byte, QUEUE_SIZE), - IdGen: idgen.NewObjectId(), } if err = client.connect(); err != nil { return @@ -191,7 +188,7 @@ func (client *Client) handleInner(key string, resp *Response) { // Internal do func (client *Client) do(funcname string, data []byte, flag uint32) (handle string, err error) { - id := client.IdGen.Id().(string) + id := IdGen.Id() req := getJob(id, []byte(funcname), data) req.DataType = flag client.write(req) diff --git a/client/client_test.go b/client/client_test.go index 1a8c13c..8057d61 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -35,8 +35,12 @@ func TestClientEcho(t *testing.T) { } func TestClientDoBg(t *testing.T) { - if handle := client.DoBg("ToUpper", []byte("abcdef"), - JOB_LOW); handle == "" { + handle, err := client.DoBg("ToUpper", []byte("abcdef"), JOB_LOW) + if err != nil { + t.Error(err) + return + } + if handle == "" { t.Error("Handle is empty.") } else { t.Log(handle) @@ -53,8 +57,13 @@ func TestClientDo(t *testing.T) { } return } - if handle := client.Do("ToUpper", []byte("abcdef"), - JOB_LOW, jobHandler); handle == "" { + handle, err := client.Do("ToUpper", []byte("abcdef"), + JOB_LOW, jobHandler) + if err != nil { + t.Error(err) + return + } + if handle == "" { t.Error("Handle is empty.") } else { t.Log(handle) @@ -76,7 +85,11 @@ func TestClientStatus(t *testing.T) { return } - handle := client.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil) + handle, err := client.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil) + if err != nil { + t.Error(err) + return + } status, err = client.Status(handle) if err != nil { t.Error(err) diff --git a/client/id.go b/client/id.go new file mode 100644 index 0000000..b3f878b --- /dev/null +++ b/client/id.go @@ -0,0 +1,37 @@ +package client + +import ( + "time" + "strconv" + "sync/atomic" +) + +var ( + IdGen IdGenerator +) + +func init() { + IdGen = NewAutoIncId() +} + +type IdGenerator interface { + Id() string +} + +// AutoIncId +type autoincId struct { + value int64 +} + +func (ai *autoincId) Id() string { + next := atomic.AddInt64(&ai.value, 1) + return strconv.FormatInt(next, 10) +} + +func NewAutoIncId() IdGenerator { + // we'll consider the nano fraction of a second at startup unique + // and count up from there. + return &autoincId{ + value: int64(time.Now().Nanosecond()) << 32, + } +} diff --git a/client/id_test.go b/client/id_test.go new file mode 100644 index 0000000..399d217 --- /dev/null +++ b/client/id_test.go @@ -0,0 +1,18 @@ +package client + +import ( + "testing" +) + +func TestAutoInc(t *testing.T) { + ai := NewAutoIncId() + + previous := ai.Id() + for i := 0; i < 10; i++ { + id := ai.Id() + if id == previous { + t.Errorf("Id not unique, previous and current %s", id) + } + previous = id + } +} diff --git a/client/pool_test.go b/client/pool_test.go index a44133e..d74e5a5 100644 --- a/client/pool_test.go +++ b/client/pool_test.go @@ -39,8 +39,13 @@ func TestPoolEcho(t *testing.T) { } func TestPoolDoBg(t *testing.T) { - if addr, handle := pool.DoBg("ToUpper", []byte("abcdef"), - JOB_LOW); handle == "" { + addr, handle, err := pool.DoBg("ToUpper", + []byte("abcdef"), JOB_LOW); + if err != nil { + t.Error(err) + return + } + if handle == "" { t.Error("Handle is empty.") } else { t.Log(addr, handle) @@ -57,8 +62,12 @@ func TestPoolDo(t *testing.T) { } return } - if addr, handle := pool.Do("ToUpper", []byte("abcdef"), - JOB_LOW, jobHandler); handle == "" { + addr, handle, err := pool.Do("ToUpper", + []byte("abcdef"), JOB_LOW, jobHandler) + if err != nil { + t.Error(err) + } + if handle == "" { t.Error("Handle is empty.") } else { t.Log(addr, handle) @@ -77,7 +86,12 @@ func TestPoolStatus(t *testing.T) { if status.Running { t.Errorf("The job (%s) shouldn't be running.", status.Handle) } - addr, handle := pool.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil) + addr, handle, err := pool.Do("Delay5sec", + []byte("abcdef"), JOB_LOW, nil) + if err != nil { + t.Error(err) + return + } status, err = pool.Status(addr, handle) if err != nil { t.Error(err)