forked from yuxh/gearman-go
Native way is better than c-wrap.
This commit is contained in:
parent
d7a1ea0e6b
commit
364744ff92
@ -5,10 +5,10 @@
|
|||||||
include $(GOROOT)/src/Make.inc
|
include $(GOROOT)/src/Make.inc
|
||||||
|
|
||||||
TARG=gearman
|
TARG=gearman
|
||||||
CGOFILES=\
|
GOFILES=\
|
||||||
gearman.go\
|
gearman.go\
|
||||||
worker.go\
|
worker.go\
|
||||||
client.go\
|
# client.go\
|
||||||
|
|
||||||
CLEANFILES+=gearman_test
|
CLEANFILES+=gearman_test
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package gearman
|
package gearman
|
||||||
|
|
||||||
// #cgo: LDFLAGS: -lgearman
|
const (
|
||||||
// #include <libgearman/gearman.h>
|
TCP = "tcp4"
|
||||||
|
WORKER_SERVER_CAP = 32
|
||||||
import "C"
|
)
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
package main
|
package gearman
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gearman"
|
"testing"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func TestAddServer(t *testing.T) {
|
||||||
worker := gearman.GearmanWorkerCreate()
|
worker := NewWorker()
|
||||||
worker.AddServer("127.0.0.1", 4730)
|
t.Log("Add local server 127.0.0.1:4730.")
|
||||||
worker.Work()
|
if err := worker.AddServer("127.0.0.1:4730"); err != nil {
|
||||||
worker.Free()
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if l := len(worker.servers); l != 1 {
|
||||||
|
t.Log(worker.servers)
|
||||||
|
t.Error("The length of server list should be 1.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,62 +1,54 @@
|
|||||||
package gearman
|
package gearman
|
||||||
|
|
||||||
// #cgo LDFLAGS: -lgearman
|
|
||||||
// #include <libgearman/gearman.h>
|
|
||||||
import "C"
|
|
||||||
|
|
||||||
import(
|
import(
|
||||||
"log"
|
"net"
|
||||||
"unsafe"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GearmanWorker struct {
|
type Worker struct {
|
||||||
worker C.gearman_worker_st
|
|
||||||
}
|
|
||||||
// Create gearman worker
|
|
||||||
func GearmanWorkerCreate() * GearmanWorker {
|
|
||||||
|
|
||||||
worker := new(GearmanWorker)
|
servers []net.Conn
|
||||||
if C.gearman_worker_create(&worker.worker) == nil {
|
}
|
||||||
log.Panic("Memory allocation failure on worker creation")
|
|
||||||
}
|
func NewWorker() (worker *Worker) {
|
||||||
|
worker = &Worker{servers:make([]net.Conn, 0, WORKER_SERVER_CAP)}
|
||||||
return worker
|
return worker
|
||||||
}
|
}
|
||||||
|
|
||||||
// get error
|
|
||||||
func (worker * GearmanWorker) Error() string {
|
|
||||||
return C.GoString(C.gearman_worker_error(&worker.worker))
|
|
||||||
}
|
|
||||||
|
|
||||||
// add server
|
// add server
|
||||||
func (worker * GearmanWorker) AddServer(host string, port uint16) {
|
// worker.AddServer("127.0.0.1:4730")
|
||||||
h := C.CString(host)
|
func (worker * Worker) AddServer(addr string) (err os.Error) {
|
||||||
defer C.free(unsafe.Pointer(h))
|
if len(worker.servers) == cap(worker.servers) {
|
||||||
if C.gearman_worker_add_server(&worker.worker, h,
|
return os.NewError("There were too many servers.")
|
||||||
C.in_port_t(port)) != C.GEARMAN_SUCCESS {
|
|
||||||
log.Panic(worker.Error())
|
|
||||||
}
|
}
|
||||||
|
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
|
// add function
|
||||||
func (worker * GearmanWorker) AddFunction(funcname string, timeout uint32, f interface{}, context interface{}) {
|
func (worker * Worker) AddFunction(funcname string,
|
||||||
fn := C.CString(funcname)
|
f interface{}, context interface{}) (err Error) {
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// work
|
// work
|
||||||
func (worker * GearmanWorker) Work() {
|
func (worker * GearmanWorker) Work() {
|
||||||
for {
|
for {
|
||||||
if C.gearman_worker_work(&worker.worker) != C.GEARMAN_SUCCESS {
|
|
||||||
log.Panic(worker.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// free
|
// Close
|
||||||
// should used as defer
|
// should used as defer
|
||||||
func (worker * GearmanWorker) Free() {
|
func (worker * GearmanWorker) Close() (err Error){
|
||||||
C.gearman_worker_free(&worker.worker)
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user