Ejemplo n.º 1
0
func sendGet(key string) {
	req := gomemcached.MCRequest{
		Opcode:  gomemcached.GET,
		Cas:     938424885,
		Opaque:  7242,
		VBucket: 824,
		Extras:  []byte{},
		Key:     []byte(key),
		Body:    []byte{},
	}

	conn, _ := net.Dial("tcp", "localhost:9955")

	conn.Write(req.Bytes())

	res := gomemcached.MCResponse{}
	_, err := res.Receive(bufio.NewReader(conn), nil)
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
	fmt.Println(res.String())
	value := string(res.Body[:len(res.Body)])
	fmt.Println(value)
	conn.Close()

}
Ejemplo n.º 2
0
func (mc *Client) sendAck(pkt *gomemcached.MCRequest) (int, error) {
	res := gomemcached.MCResponse{
		Opcode: pkt.Opcode,
		Opaque: pkt.Opaque,
		Status: gomemcached.SUCCESS,
	}
	return res.Transmit(mc.conn)
}
Ejemplo n.º 3
0
func transmitResponse(o io.Writer, res *gomemcached.MCResponse) (err error) {
	if len(res.Body) < 128 {
		_, err = o.Write(res.Bytes())
	} else {
		_, err = o.Write(res.HeaderBytes())
		if err == nil && len(res.Body) > 0 {
			_, err = o.Write(res.Body)
		}
	}
	return
}
Ejemplo n.º 4
0
func vbRGet(v *VBucket, w io.Writer, req *gomemcached.MCRequest) (
	res *gomemcached.MCResponse) {
	// From http://code.google.com/p/memcached/wiki/RangeOps
	// Extras field  Bits
	// ------------------
	// End key len	 16
	// Reserved       8
	// Flags          8
	// Max results	 32

	res = &gomemcached.MCResponse{
		Opcode: req.Opcode,
		Cas:    req.Cas,
	}

	extras := make([]byte, 4)

	visitRGetResults := int64(0)
	visitOutgoingValueBytes := int64(0)

	visitor := func(i *item) bool {
		if bytes.Compare(i.key, req.Key) >= 0 {
			// TODO: Need to hide expired items from range scan.
			binary.BigEndian.PutUint32(extras, i.flag)
			r := gomemcached.MCResponse{
				Opcode: req.Opcode,
				Key:    i.key,
				Cas:    i.cas,
				Extras: extras,
				Body:   i.data,
			}
			err := r.Transmit(w)
			if err != nil {
				res = &gomemcached.MCResponse{Fatal: true}
				return false
			}
			visitRGetResults++
			visitOutgoingValueBytes += int64(len(i.data))
		}
		return true
	}

	if err := v.ps.visitItems(req.Key, true, visitor); err != nil {
		res = &gomemcached.MCResponse{Fatal: true}
	}

	atomic.AddInt64(&v.stats.RGets, 1)
	atomic.AddInt64(&v.stats.RGetResults, visitRGetResults)
	atomic.AddInt64(&v.stats.OutgoingValueBytes, visitOutgoingValueBytes)

	return res
}
Ejemplo n.º 5
0
func sendMalformedCommand() {
	req := "HERE ARE SOME BYTES TO SEE IF YOUR SERVER CAN HANDLE THEM!"

	conn, _ := net.Dial("tcp", "localhost:9955")

	conn.Write([]byte(req))

	res := gomemcached.MCResponse{}
	_, err := res.Receive(bufio.NewReader(conn), nil)
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
	fmt.Println(res.String())
	conn.Close()

}
Ejemplo n.º 6
0
func BenchmarkTransmitResNull(b *testing.B) {
	res := gomemcached.MCResponse{
		Opcode: gomemcached.SET,
		Cas:    938424885,
		Opaque: 7242,
		Status: 824,
		Key:    []byte("somekey"),
		Body:   []byte("somevalue"),
	}

	b.SetBytes(int64(res.Size()))

	for i := 0; i < b.N; i++ {
		err := transmitResponse(ioutil.Discard, &res)
		if err != nil {
			b.Fatalf("Error transmitting request: %v", err)
		}
	}
}
Ejemplo n.º 7
0
func TestTransmitRes(t *testing.T) {
	b := bytes.NewBuffer([]byte{})
	buf := bufio.NewWriter(b)

	res := gomemcached.MCResponse{
		Opcode: gomemcached.SET,
		Cas:    938424885,
		Opaque: 7242,
		Status: 0x338,
		Extras: []byte{},
		Key:    []byte("somekey"),
		Body:   []byte("somevalue"),
	}

	err := transmitResponse(buf, &res)
	if err != nil {
		t.Fatalf("Error transmitting request: %v", err)
	}

	buf.Flush()

	expected := []byte{
		gomemcached.RES_MAGIC, byte(gomemcached.SET),
		0x0, 0x7, // length of key
		0x0,       // extra length
		0x0,       // reserved
		0x3, 0x38, // Status
		0x0, 0x0, 0x0, 0x10, // Length of value
		0x0, 0x0, 0x1c, 0x4a, // opaque
		0x0, 0x0, 0x0, 0x0, 0x37, 0xef, 0x3a, 0x35, // CAS
		's', 'o', 'm', 'e', 'k', 'e', 'y',
		's', 'o', 'm', 'e', 'v', 'a', 'l', 'u', 'e'}

	if len(b.Bytes()) != res.Size() {
		t.Fatalf("Expected %v bytes, got %v", res.Size(),
			len(b.Bytes()))
	}

	if !reflect.DeepEqual(b.Bytes(), expected) {
		t.Fatalf("Expected:\n%#v\n  -- got -- \n%#v",
			expected, b.Bytes())
	}
}
Ejemplo n.º 8
0
func BenchmarkReceive(b *testing.B) {
	res := gomemcached.MCResponse{
		Opcode: gomemcached.SET,
		Cas:    938424885,
		Opaque: 7242,
		Status: 824,
		Key:    []byte("somekey"),
		Body:   []byte("somevalue"),
	}

	datum := res.Bytes()
	datum[0] = gomemcached.REQ_MAGIC
	b.SetBytes(int64(len(datum)))

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := ReadPacket(bytes.NewReader(datum))
		if err != nil {
			b.Fatalf("Failed to read: %v", err)
		}
	}
}
Ejemplo n.º 9
0
func BenchmarkTransmitResLarge(b *testing.B) {
	bout := &bytes.Buffer{}

	res := gomemcached.MCResponse{
		Opcode: gomemcached.SET,
		Cas:    938424885,
		Opaque: 7242,
		Status: 824,
		Key:    []byte("somekey"),
		Body:   make([]byte, 24*1024),
	}

	b.SetBytes(int64(res.Size()))

	for i := 0; i < b.N; i++ {
		bout.Reset()
		buf := bufio.NewWriterSize(bout, res.Size()*2)
		err := transmitResponse(buf, &res)
		if err != nil {
			b.Fatalf("Error transmitting request: %v", err)
		}
	}
}
Ejemplo n.º 10
0
func sendUnknownCommand() {
	req := gomemcached.MCRequest{
		Opcode:  gomemcached.ADD,
		Cas:     938424885,
		Opaque:  7242,
		VBucket: 824,
		Extras:  []byte{},
		Key:     []byte("key"),
		Body:    []byte("somevalue"),
	}

	conn, _ := net.Dial("tcp", "localhost:9955")

	conn.Write(req.Bytes())

	res := gomemcached.MCResponse{}
	_, err := res.Receive(bufio.NewReader(conn), nil)
	if err != nil {
		fmt.Println("Error: ", err.Error())
	}
	fmt.Println(res.String())
	conn.Close()

}
Ejemplo n.º 11
0
func notFound(req *gomemcached.MCRequest, s *storage) *gomemcached.MCResponse {
	var response gomemcached.MCResponse
	response.Status = gomemcached.UNKNOWN_COMMAND
	return &response
}
Ejemplo n.º 12
0
func transmitResponse(o io.Writer, res *gomemcached.MCResponse) (err error) {
	return res.Transmit(o)
}
Ejemplo n.º 13
0
func transmitResponse(s io.Writer, res *gomemcached.MCResponse) error {
	_, err := s.Write(res.Bytes())
	return err
}