go-examples/example-redis/main.go

157 lines
2.5 KiB
Go
Raw Normal View History

2017-07-09 14:50:55 +00:00
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"net"
"strings"
)
2017-07-09 16:46:32 +00:00
type RedisCmd struct {
cmd string
key string
val string
}
type CommandExec struct {
*RedisCmd
Result chan string
}
2017-07-09 14:50:55 +00:00
func getCommand(reader io.Reader) (string, error) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
str := scanner.Text()
return str, nil
}
return "", scanner.Err()
}
func isCommand(str string) bool {
switch str {
case "GET":
fallthrough
case "SET":
fallthrough
case "DEL":
return true
default:
return false
}
}
2017-07-09 16:46:32 +00:00
func parseCmd(line string) (*RedisCmd, error) {
redisCmd := new(RedisCmd)
2017-07-09 14:50:55 +00:00
var err error
parts := strings.Split(line, " ")
if len(parts) != 2 && len(parts) != 3 {
err = errors.New("Invalid number of arguments")
} else {
for i, part := range parts {
switch i {
case 0:
if isCommand(part) {
2017-07-09 16:46:32 +00:00
redisCmd.cmd = part
2017-07-09 14:50:55 +00:00
} else {
err = errors.New("Unknown command")
break
}
case 1:
2017-07-09 16:46:32 +00:00
redisCmd.key = part
2017-07-09 14:50:55 +00:00
case 2:
2017-07-09 16:46:32 +00:00
if redisCmd.cmd == "SET" {
redisCmd.val = part
2017-07-09 14:50:55 +00:00
} else {
err = errors.New("Invalid number of arguments")
}
}
}
}
2017-07-09 16:46:32 +00:00
return redisCmd, err
2017-07-09 14:50:55 +00:00
}
func respond(writer io.Writer, ret string) {
_, err := io.WriteString(writer, ret+"\n")
if err != nil {
log.Println(err)
}
}
func main() {
db := make(map[string]string)
ln, err := net.Listen("tcp", ":9000")
if err != nil {
panic(err)
}
defer ln.Close()
2017-07-09 16:46:32 +00:00
commands := make(chan CommandExec)
go func() {
for exec := range commands {
switch exec.cmd {
case "DEL":
log.Println("Deleting", exec.key)
delete(db, exec.key)
close(exec.Result)
case "GET":
log.Println("Getting", exec.key)
ret, ok := db[exec.key]
if ok {
log.Println("Value found", ret)
go func() {
exec.Result <- ret
}()
} else {
log.Println("Unknown key", exec.key)
close(exec.Result)
}
case "SET":
log.Println("Setting", exec.key)
db[exec.key] = exec.val
close(exec.Result)
}
}
}()
2017-07-09 14:50:55 +00:00
for {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
2017-07-09 16:46:32 +00:00
results := make(chan string)
go func() {
ret, ok := <-results
if ok {
respond(conn, ret)
}
defer conn.Close()
}()
2017-07-09 14:50:55 +00:00
go func() {
2017-07-09 16:46:32 +00:00
exec := new(CommandExec)
exec.Result = results
2017-07-09 14:50:55 +00:00
line, err := getCommand(conn)
if err != nil {
2017-07-09 16:46:32 +00:00
go func() {
exec.Result <- fmt.Sprint(err)
}()
2017-07-09 14:50:55 +00:00
}
2017-07-09 16:46:32 +00:00
cmd, err := parseCmd(line)
2017-07-09 14:50:55 +00:00
if err != nil {
2017-07-09 16:46:32 +00:00
go func() {
exec.Result <- fmt.Sprint(err)
}()
2017-07-09 14:50:55 +00:00
}
2017-07-09 16:46:32 +00:00
exec.RedisCmd = cmd
commands <- *exec
2017-07-09 14:50:55 +00:00
}()
2017-07-09 16:46:32 +00:00
2017-07-09 14:50:55 +00:00
}
}