2014-10-29 04:11:22 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-10-29 19:01:19 +03:00
|
|
|
"github.com/siddontang/go/arena"
|
2014-10-29 04:11:22 +03:00
|
|
|
"github.com/siddontang/ledisdb/server"
|
|
|
|
"net"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var addr = flag.String("addr", ":6380", "listen addr")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
l, err := net.Listen("tcp", *addr)
|
|
|
|
|
|
|
|
println("listen", *addr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
println(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
go run(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(c net.Conn) {
|
|
|
|
//buf := make([]byte, 10240)
|
|
|
|
ok := []byte("+OK\r\n")
|
2014-10-29 19:01:19 +03:00
|
|
|
data := []byte("$4096\r\n")
|
|
|
|
data = append(data, make([]byte, 4096)...)
|
|
|
|
data = append(data, "\r\n"...)
|
2014-10-29 04:11:22 +03:00
|
|
|
|
|
|
|
var rt time.Duration
|
|
|
|
var wt time.Duration
|
|
|
|
|
|
|
|
rb := bufio.NewReaderSize(c, 10240)
|
|
|
|
wb := bufio.NewWriterSize(c, 10240)
|
|
|
|
|
2014-10-29 19:01:19 +03:00
|
|
|
a := arena.NewArena(10240)
|
|
|
|
|
2014-10-29 04:11:22 +03:00
|
|
|
for {
|
|
|
|
t1 := time.Now()
|
|
|
|
|
2014-10-29 19:01:19 +03:00
|
|
|
a.Reset()
|
|
|
|
|
|
|
|
req, err := server.ReadRequest(rb, a)
|
2014-10-29 04:11:22 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t2 := time.Now()
|
|
|
|
|
|
|
|
rt += t2.Sub(t1)
|
|
|
|
|
|
|
|
cmd := string(bytes.ToUpper(req[0]))
|
|
|
|
switch cmd {
|
|
|
|
case "SET":
|
|
|
|
wb.Write(ok)
|
|
|
|
case "GET":
|
2014-10-29 19:01:19 +03:00
|
|
|
wb.Write(data)
|
2014-10-29 04:11:22 +03:00
|
|
|
default:
|
|
|
|
wb.WriteString(fmt.Sprintf("-Err %s Not Supported Now", req[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.Flush()
|
|
|
|
|
|
|
|
t3 := time.Now()
|
|
|
|
wt += t3.Sub(t2)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("rt:%s wt:%s\n", rt.String(), wt.String())
|
|
|
|
}
|