Skip to content

Commit

Permalink
Upgraded Go version to Go v1. Ran gofmt on it
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamking committed Mar 21, 2012
1 parent dddcf3b commit babbc22
Showing 1 changed file with 85 additions and 85 deletions.
170 changes: 85 additions & 85 deletions memg.go
Original file line number Diff line number Diff line change
@@ -1,110 +1,110 @@
#!/usr/bin/env gorun
package main;
package main

import (
"fmt"
"net"
"bufio"
"strings"
"os"
"strconv"
//"runtime/pprof" // Uncomment to profile
"bufio"
"fmt"
"io"
"net"
"os"
"strconv"
"strings"
//"runtime/pprof" // Uncomment to profile
)

var CACHE map[string] string;
var CACHE map[string]string

func main() {

// Uncomment these three lines to profile
/*
handle, _ := os.Create("memg.prof")
pprof.StartCPUProfile(handle)
defer pprof.StopCPUProfile()
*/
// Uncomment these three lines to profile
/*
handle, _ := os.Create("memg.prof")
pprof.StartCPUProfile(handle)
defer pprof.StopCPUProfile()
*/

listener, err := net.Listen("tcp", "127.0.0.1:11211")
if err != nil {
panic("Error listening on 11211: " + err.String())
}
listener, err := net.Listen("tcp", "127.0.0.1:11211")
if err != nil {
panic("Error listening on 11211: " + err.Error())
}

CACHE = make(map[string] string)
CACHE = make(map[string]string)

if isSingle() {
netconn, err := listener.Accept()
if err != nil {
panic("Accept error: " + err.String())
}
if isSingle() {
netconn, err := listener.Accept()
if err != nil {
panic("Accept error: " + err.Error())
}

handleConn(netconn)
handleConn(netconn)

} else {
for {
netconn, err := listener.Accept()
if err != nil {
panic("Accept error: " + err.String())
}
} else {
for {
netconn, err := listener.Accept()
if err != nil {
panic("Accept error: " + err.Error())
}

go handleConn(netconn)
}
}
go handleConn(netconn)
}
}

}

func isSingle() bool {
for _, arg := range os.Args {
if arg == "--single" {
return true
}
}
return false
for _, arg := range os.Args {
if arg == "--single" {
return true
}
}
return false
}

/*
* Networking
*/
func handleConn(conn net.Conn) {

reader := bufio.NewReader(conn)
for {

// Fetch

content, err := reader.ReadString('\n')
if err == os.EOF {
break
} else if err != nil {
fmt.Println(err)
return
}

content = content[:len(content) - 2] // Chop \r\n

// Handle

parts := strings.Split(content, " ")
cmd := parts[0]
switch cmd {

case "get":
key := parts[1]
val, ok := CACHE[key]
if ok {
length := strconv.Itoa(len(val))
conn.Write([]uint8("VALUE " + key + " 0 " + length + "\r\n"))
conn.Write([]uint8(val + "\r\n"))
}
conn.Write([]uint8("END\r\n"))

case "set":
key := parts[1]
//exp := parts[2]
//flags := parts[3]
length, _ := strconv.Atoi(parts[4])
// Really we should read exactly 'length' bytes + \r\n
val := make([]byte, length);
reader.Read(val)
CACHE[key] = string(val);
conn.Write([]uint8("STORED\r\n"))
}
}
reader := bufio.NewReader(conn)
for {

// Fetch

content, err := reader.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
fmt.Println(err)
return
}

content = content[:len(content)-2] // Chop \r\n

// Handle

parts := strings.Split(content, " ")
cmd := parts[0]
switch cmd {

case "get":
key := parts[1]
val, ok := CACHE[key]
if ok {
length := strconv.Itoa(len(val))
conn.Write([]uint8("VALUE " + key + " 0 " + length + "\r\n"))
conn.Write([]uint8(val + "\r\n"))
}
conn.Write([]uint8("END\r\n"))

case "set":
key := parts[1]
//exp := parts[2]
//flags := parts[3]
length, _ := strconv.Atoi(parts[4])
// Really we should read exactly 'length' bytes + \r\n
val := make([]byte, length)
reader.Read(val)
CACHE[key] = string(val)
conn.Write([]uint8("STORED\r\n"))
}
}
}

0 comments on commit babbc22

Please sign in to comment.