From 7a8a5d4a6a4581efb272db171e860e90e3df269d Mon Sep 17 00:00:00 2001 From: mikespook Date: Thu, 28 Feb 2013 13:28:48 +0800 Subject: [PATCH] for issue #10; an IdGenerator interface, use bson.ObjectId as default --- client/client.go | 7 +++---- client/id.go | 37 +++++++++++++++++++++++++++++++++++++ example/client.go | 27 ++++++++++----------------- 3 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 client/id.go diff --git a/client/client.go b/client/client.go index dd64137..2cbd39b 100644 --- a/client/client.go +++ b/client/client.go @@ -12,16 +12,15 @@ import ( "time" "bytes" "strconv" - "github.com/mikespook/golib/autoinc" "github.com/mikespook/gearman-go/common" ) var ( - ai *autoinc.AutoInc + IdGen IdGenerator ) func init() { - ai = autoinc.New(0, 1) + IdGen = NewObjectId() } // Status handler @@ -316,7 +315,7 @@ flag byte, jobhandler JobHandler) (handle string) { default: datatype = common.SUBMIT_JOB } - id := strconv.Itoa(int(ai.Id())) + id := IdGen.Id() handle = client.do(funcname, data, datatype, id) if jobhandler != nil { client.jobhandlers[id] = jobhandler diff --git a/client/id.go b/client/id.go new file mode 100644 index 0000000..e55c911 --- /dev/null +++ b/client/id.go @@ -0,0 +1,37 @@ +package client + +import ( + "strconv" + "labix.org/v2/mgo/bson" + "github.com/mikespook/golib/autoinc" +) + +type IdGenerator interface { + Id() string +} + +// ObjectId +type objectId struct { + bson.ObjectId +} + +func (id *objectId) Id() string { + return id.String() +} + +func NewObjectId() IdGenerator { + return &objectId{bson.NewObjectId()} +} + +// AutoIncId +type autoincId struct { + *autoinc.AutoInc +} + +func (id *autoincId) Id() string { + return strconv.Itoa(id.AutoInc.Id()) +} + +func NewAutoIncId() IdGenerator { + return &autoincId{autoinc.New(1, 1)} +} diff --git a/example/client.go b/example/client.go index 43ef1e4..f7cdfa4 100644 --- a/example/client.go +++ b/example/client.go @@ -8,39 +8,32 @@ import ( func main() { var wg sync.WaitGroup + // Set the autoinc id generator + // You can write your own id generator + // by implementing IdGenerator interface. + client.IdGen = client.NewAutoIncId() c, err := client.New("127.0.0.1:4730") if err != nil { log.Fatalln(err) } defer c.Close() - echo := []byte("Hello\x00 world") - c.JobHandler = func(job *client.Job) error { - log.Printf("%s", job.Data) - wg.Done() - return nil - } - c.ErrHandler = func(e error) { log.Println(e) panic(e) } + echo := []byte("Hello\x00 world") wg.Add(1) c.Echo(echo) wg.Add(1) - handle, err := c.Do("ToUpper", echo, client.JOB_NORMAL) - if err != nil { - log.Fatalln(err) - } else { - log.Println(handle) - } - - c.StatusHandler = func(handle string, known, running bool, numerator, denominator uint64) { - log.Printf("%s: %b, %b, %d, %d", handle, known, running, numerator, denominator) + jobHandler := func(job *client.Job) { + log.Printf("%s", job.Data) wg.Done() } + handle := c.Do("ToUpper", echo, client.JOB_NORMAL, jobHandler) + wg.Add(1) - c.Status(handle) + log.Printf("%t", c.Status(handle)) wg.Wait() }