Exemplo n.º 1
0
func conHandler(c net.Conn) {
	defer c.Close()
	fmt.Printf("[counterproxy] - Accept connect: %v\n", c)

	mc, err := libmemcached.Connect("localhost", "11211")
	if err != nil {
		fmt.Println(err)
		fmt.Printf("[counterproxy] - Connect Memcached Server failed!\n")
		return
	}
	defer libmemcached.Close(mc)

	var buf [512]byte
	rb := ringbuf.New(512)

	for {
		n, err := c.Read(buf[:])

		if err != nil {
			fmt.Println(err)
			if err == io.EOF {
				fmt.Printf("[counterproxy] - Client [%v] shutdown!\n", c)
			}
			break
		}

		_, err = rb.Write(buf[:], n)
		if err != nil {
			fmt.Println(err)
			break
		}

		packages, err := proto.Parse(rb)
		if err != nil && err != proto.ErrInComplete {
			fmt.Println(err)
			fmt.Printf("[counterproxy] - Close the connection: %v\n", c)
			return
		}

		for i := range packages {
			t := time.Now()
			tmstamp := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
			out, err := incr(mc, packages[i].Key, packages[i].Val, tmstamp, EXPIRE_TIME)
			if err != nil {
				fmt.Println(err)
				fmt.Printf("[counterproxy] - Incr err!\n")
				return
			}

			_, err = c.Write([]byte(tmstamp + " " + out + "\r\n"))
			if err != nil {
				fmt.Println(err)
				fmt.Printf("[counterproxy] - Send response err!\n")
				return
			}
		}
	}
}
func BenchmarkIncr(b *testing.B) {
	b.StopTimer()
	//mc, err := libmemcached.Connect("10.10.126.187", "11212")
	mc, err := libmemcached.Connect("localhost", "11211")
	if err != nil {
		fmt.Println(err)
		fmt.Printf("[counterproxy] - Connect Memcached Server failed!\n")
		return
	}
	defer libmemcached.Close(mc)
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		_, err := incr(mc, "12580", "1", "120")
		if err != nil {
			fmt.Println(err)
			fmt.Printf("[counterproxy] - Incr err!\n")
			return
		}
		//        fmt.Println("new value = ", out)
	}
}