From 364744ff92b4dce667e1fa5d24b566b5e115ecc7 Mon Sep 17 00:00:00 2001 From: mikespook Date: Mon, 16 May 2011 17:26:32 +0800 Subject: [PATCH] Native way is better than c-wrap. --- src/pkg/gearman/Makefile | 4 +- src/pkg/gearman/gearman.go | 8 ++-- src/pkg/gearman/gearman_test.go | 21 +++++++---- src/pkg/gearman/worker.go | 66 +++++++++++++++------------------ 4 files changed, 48 insertions(+), 51 deletions(-) diff --git a/src/pkg/gearman/Makefile b/src/pkg/gearman/Makefile index 147ef13..c6b451d 100644 --- a/src/pkg/gearman/Makefile +++ b/src/pkg/gearman/Makefile @@ -5,10 +5,10 @@ include $(GOROOT)/src/Make.inc TARG=gearman -CGOFILES=\ +GOFILES=\ gearman.go\ worker.go\ - client.go\ +# client.go\ CLEANFILES+=gearman_test diff --git a/src/pkg/gearman/gearman.go b/src/pkg/gearman/gearman.go index 6731e83..1740203 100644 --- a/src/pkg/gearman/gearman.go +++ b/src/pkg/gearman/gearman.go @@ -1,6 +1,6 @@ package gearman -// #cgo: LDFLAGS: -lgearman -// #include - -import "C" +const ( + TCP = "tcp4" + WORKER_SERVER_CAP = 32 +) diff --git a/src/pkg/gearman/gearman_test.go b/src/pkg/gearman/gearman_test.go index e8c3007..de669cb 100644 --- a/src/pkg/gearman/gearman_test.go +++ b/src/pkg/gearman/gearman_test.go @@ -1,13 +1,18 @@ -package main +package gearman import ( - "gearman" - "fmt" + "testing" ) -func main() { - worker := gearman.GearmanWorkerCreate() - worker.AddServer("127.0.0.1", 4730) - worker.Work() - worker.Free() +func TestAddServer(t *testing.T) { + worker := NewWorker() + t.Log("Add local server 127.0.0.1:4730.") + if err := worker.AddServer("127.0.0.1:4730"); err != nil { + t.Error(err) + } + + if l := len(worker.servers); l != 1 { + t.Log(worker.servers) + t.Error("The length of server list should be 1.") + } } diff --git a/src/pkg/gearman/worker.go b/src/pkg/gearman/worker.go index 4d15871..810c2b8 100644 --- a/src/pkg/gearman/worker.go +++ b/src/pkg/gearman/worker.go @@ -1,62 +1,54 @@ package gearman -// #cgo LDFLAGS: -lgearman -// #include -import "C" - import( - "log" - "unsafe" + "net" + "os" ) -type GearmanWorker struct { - worker C.gearman_worker_st -} -// Create gearman worker -func GearmanWorkerCreate() * GearmanWorker { +type Worker struct { - worker := new(GearmanWorker) - if C.gearman_worker_create(&worker.worker) == nil { - log.Panic("Memory allocation failure on worker creation") - } + servers []net.Conn +} + +func NewWorker() (worker *Worker) { + worker = &Worker{servers:make([]net.Conn, 0, WORKER_SERVER_CAP)} return worker } -// get error -func (worker * GearmanWorker) Error() string { - return C.GoString(C.gearman_worker_error(&worker.worker)) -} // add server -func (worker * GearmanWorker) AddServer(host string, port uint16) { - h := C.CString(host) - defer C.free(unsafe.Pointer(h)) - if C.gearman_worker_add_server(&worker.worker, h, - C.in_port_t(port)) != C.GEARMAN_SUCCESS { - log.Panic(worker.Error()) +// worker.AddServer("127.0.0.1:4730") +func (worker * Worker) AddServer(addr string) (err os.Error) { + if len(worker.servers) == cap(worker.servers) { + return os.NewError("There were too many servers.") } + conn, err := net.Dial(TCP, addr) + if err != nil { + return err + } + n := len(worker.servers) + worker.servers = worker.servers[0: n + 1] + worker.servers[n] = conn + return nil } +/* // add function -func (worker * GearmanWorker) AddFunction(funcname string, timeout uint32, f interface{}, context interface{}) { - fn := C.CString(funcname) - defer C.free(unsafe.Pointer(fn)) - if C.gearman_worker_add_function(&worker.worker, fn, C.uint32_t(timeout), C.gearman_worker_fn(&unsafe.Pointer(&f)), unsafe.Pointer(&context)) != C.GEARMAN_SUCCESS { - log.Panic(worker.Error()) - } +func (worker * Worker) AddFunction(funcname string, + f interface{}, context interface{}) (err Error) { + } // work func (worker * GearmanWorker) Work() { for { - if C.gearman_worker_work(&worker.worker) != C.GEARMAN_SUCCESS { - log.Panic(worker.Error()) - } + } } -// free +// Close // should used as defer -func (worker * GearmanWorker) Free() { - C.gearman_worker_free(&worker.worker) +func (worker * GearmanWorker) Close() (err Error){ + } +*/