Beispiel #1
0
func (self *memcached) AddServer(host string, port int, weight uint32) error {
	cs_host := C.CString(host)
	defer C.free(unsafe.Pointer(cs_host))
	return self.checkError(
		C.memcached_server_add_with_weight(
			self.mc, cs_host, C.in_port_t(port), C.uint32_t(weight)))
}
Beispiel #2
0
func (gc *GearmanClient) AddServer(host string, port int) int {
	var ret C.gearman_return_t

	ret = C.gearman_client_add_server(gc.clientp, C.CString(host), C.in_port_t(port))

	fmt.Println("ret:", ret, int(ret) == GEARMAN_SUCCESS)
	return int(ret)
	// return GEARMAN_SUCCESS;
}
Beispiel #3
0
// NewClient initializes a hyperdex client ready to use.
//
// For every call to NewClient, there must be a call to Destroy.
//
// Panics when the internal looping goroutine receives an error from hyperdex.
//
// Example:
// 		client, err := hyperclient.NewClient("127.0.0.1", 1234)
// 		if err != nil {
//			//handle error
//		}
//		defer client.Destroy()
//		// use client
func NewClient(ip string, port int) (*Client, error) {
	C_client := C.hyperclient_create(C.CString(ip), C.in_port_t(port))
	//log.Printf("hyperclient_create(\"%s\", %d) -> %X\n", ip, port, unsafe.Pointer(C_client))
	if C_client == nil {
		return nil, fmt.Errorf("Could not create hyperclient (ip=%s, port=%d)", ip, port)
	}
	client := &Client{
		C_client,
		make([]request, 0, 8), // No reallocation within 8 concurrent requests to hyperclient_loop
		make(chan struct{}, 1),
	}

	go func() {
		for {
			select {
			// quit goroutine when client is destroyed
			case <-client.closeChan:
				return
			default:
				// check if there are pending requests
				// and only if there are, call hyperclient_loop
				if l := len(client.requests); l > 0 {
					var status C.enum_hyperclient_returncode = 42
					ret := int64(C.hyperclient_loop(client.ptr, C.int(TIMEOUT), &status))
					//log.Printf("hyperclient_loop(%X, %d, %X) -> %d\n", unsafe.Pointer(client.ptr), hyperclient_loop_timeout, unsafe.Pointer(&status), ret)
					if ret < 0 {
						panic(newInternalError(status).Error())
					}
					// find processed request among pending requests
					for i, req := range client.requests {
						if req.id == ret {
							if status == returncode_SUCCESS {
								req.success(req)
							} else {
								req.failure(req, status)
							}
							if req.complete != nil {
								req.complete(req)
							}
							// remove processed request from pending requests
							client.requests = append(client.requests[:i], client.requests[i+1:]...)
							break
						}
					}
				}
				// prevent other goroutines from starving
				runtime.Gosched()
			}
		}
		panic("Should not be reached: end of infinite loop")
	}()

	return client, nil
}