Пример #1
0
// Version will return a map reflecting versions of each memcached server
func (client *Client) Version() (map[string]string, error) {
	client.lock()
	defer client.unlock()

	var rst *C.broadcast_result_t
	var n C.size_t
	rv := make(map[string]string)

	errCode := C.client_version(client._imp, &rst, &n)
	defer C.client_destroy_broadcast_result(client._imp)
	sr := unsafe.Sizeof(*rst)

	for i := 0; i < int(n); i++ {
		if rst.lines == nil || rst.line_lens == nil {
			continue
		}

		host := C.GoString(rst.host)
		version := C.GoStringN(*rst.lines, C.int(*rst.line_lens))
		rv[host] = version
		rst = (*C.broadcast_result_t)(unsafe.Pointer(uintptr(unsafe.Pointer(rst)) + sr))
	}

	if errCode != 0 {
		return rv, networkError(errorMessage[errCode])
	}

	return rv, nil
}
Пример #2
0
// Quit will close the sockets to each memcached server
func (client *Client) Quit() error {
	client.lock()
	defer client.unlock()

	errCode := C.client_quit(client._imp)
	C.client_destroy_broadcast_result(client._imp)

	if errCode == C.RET_CONN_POLL_ERR || errCode == C.RET_RECV_ERR {
		return nil
	}
	return networkError(errorMessage[errCode])
}
Пример #3
0
// Stats will return a map reflecting stats map of each memcached server
func (client *Client) Stats() (map[string](map[string]string), error) {
	client.lock()
	defer client.unlock()

	var rst *C.broadcast_result_t
	var n C.size_t

	errCode := C.client_stats(client._imp, &rst, &n)
	defer C.client_destroy_broadcast_result(client._imp)

	rv := make(map[string](map[string]string))

	sr := unsafe.Sizeof(*rst)

	for i := 0; i < int(n); i++ {
		if rst.lines == nil || rst.line_lens == nil {
			continue
		}
		host := C.GoString(rst.host)
		nMetrics := int(rst.len)

		rv[host] = make(map[string]string)

		cLines := rst.lines
		cLineLens := rst.line_lens

		sLine := unsafe.Sizeof(*cLines)
		sLineLen := unsafe.Sizeof(*cLineLens)
		for j := 0; j < nMetrics; j++ {
			metricLine := C.GoStringN(*cLines, C.int(*cLineLens))
			kv := strings.SplitN(metricLine, " ", 2)
			rv[host][kv[0]] = kv[1]
			cLines = (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(cLines)) + sLine))
			cLineLens = (*C.size_t)(unsafe.Pointer(uintptr(unsafe.Pointer(cLineLens)) + sLineLen))
		}

		rst = (*C.broadcast_result_t)(unsafe.Pointer(uintptr(unsafe.Pointer(rst)) + sr))
	}

	if errCode != 0 {
		return rv, networkError(errorMessage[errCode])
	}

	return rv, nil
}