evio/examples/redis-server/main.go

172 lines
4.4 KiB
Go
Raw Normal View History

2017-11-02 18:08:18 +03:00
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2017-07-04 06:39:18 +03:00
package main
import (
2017-11-02 23:29:13 +03:00
"flag"
"fmt"
2017-07-04 06:39:18 +03:00
"log"
"strings"
"sync"
2017-07-04 06:39:18 +03:00
2017-10-29 00:58:59 +03:00
"github.com/tidwall/evio"
2017-07-04 06:39:18 +03:00
"github.com/tidwall/redcon"
)
2017-10-28 22:23:13 +03:00
type conn struct {
2017-10-29 00:58:59 +03:00
is evio.InputStream
2017-10-28 22:23:13 +03:00
addr string
2017-11-06 13:58:06 +03:00
}
2017-07-04 06:39:18 +03:00
func main() {
2017-11-02 23:29:13 +03:00
var port int
var unixsocket string
var stdlib bool
var loops int
var balance string
2017-11-02 23:29:13 +03:00
flag.IntVar(&port, "port", 6380, "server port")
flag.IntVar(&loops, "loops", 0, "num loops")
2017-11-02 23:29:13 +03:00
flag.StringVar(&unixsocket, "unixsocket", "socket", "unix socket")
flag.StringVar(&balance, "balance", "random", "random, round-robin, least-connections")
flag.BoolVar(&stdlib, "stdlib", false, "use stdlib")
2017-11-02 23:29:13 +03:00
flag.Parse()
var mu sync.RWMutex
2017-07-04 06:39:18 +03:00
var keys = make(map[string]string)
2017-10-29 00:58:59 +03:00
var events evio.Events
switch balance {
default:
log.Fatalf("invalid -balance flag: '%v'", balance)
case "random":
events.LoadBalance = evio.Random
case "round-robin":
events.LoadBalance = evio.RoundRobin
case "least-connections":
events.LoadBalance = evio.LeastConnections
}
events.NumLoops = loops
events.Serving = func(srv evio.Server) (action evio.Action) {
log.Printf("redis server started on port %d (loops: %d)", port, srv.NumLoops)
2017-11-02 23:29:13 +03:00
if unixsocket != "" {
log.Printf("redis server started at %s (loops: %d)", unixsocket, srv.NumLoops)
2017-11-02 23:29:13 +03:00
}
if stdlib {
log.Printf("stdlib")
}
2017-10-28 03:01:03 +03:00
return
}
events.Opened = func(ec evio.Conn) (out []byte, opts evio.Options, action evio.Action) {
ec.SetContext(&conn{})
2017-11-06 13:58:06 +03:00
return
}
events.Closed = func(ec evio.Conn, err error) (action evio.Action) {
2017-10-28 03:01:03 +03:00
return
}
events.Data = func(ec evio.Conn, in []byte) (out []byte, action evio.Action) {
c := ec.Context().(*conn)
2017-10-28 22:23:13 +03:00
data := c.is.Begin(in)
2017-10-28 03:01:03 +03:00
var n int
var complete bool
var err error
var args [][]byte
2017-10-29 00:58:59 +03:00
for action == evio.None {
2017-10-28 03:01:03 +03:00
complete, args, _, data, err = redcon.ReadNextCommand(data, args[:0])
if err != nil {
2017-10-29 00:58:59 +03:00
action = evio.Close
2017-10-28 03:01:03 +03:00
out = redcon.AppendError(out, err.Error())
2017-07-04 06:39:18 +03:00
break
}
2017-10-28 03:01:03 +03:00
if !complete {
break
2017-07-04 06:39:18 +03:00
}
2017-10-28 03:01:03 +03:00
if len(args) > 0 {
n++
2017-07-04 06:39:18 +03:00
switch strings.ToUpper(string(args[0])) {
default:
2017-10-28 03:01:03 +03:00
out = redcon.AppendError(out, "ERR unknown command '"+string(args[0])+"'")
2017-07-04 06:39:18 +03:00
case "PING":
2017-10-28 22:34:12 +03:00
if len(args) > 2 {
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
} else if len(args) == 2 {
out = redcon.AppendBulk(out, args[1])
} else {
out = redcon.AppendString(out, "PONG")
}
case "ECHO":
if len(args) != 2 {
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
} else {
out = redcon.AppendBulk(out, args[1])
}
2017-07-04 06:39:18 +03:00
case "SHUTDOWN":
2017-10-28 03:01:03 +03:00
out = redcon.AppendString(out, "OK")
2017-10-29 00:58:59 +03:00
action = evio.Shutdown
2017-10-28 03:01:03 +03:00
case "QUIT":
out = redcon.AppendString(out, "OK")
2017-10-29 00:58:59 +03:00
action = evio.Close
2017-07-04 06:39:18 +03:00
case "GET":
if len(args) != 2 {
2017-10-28 03:01:03 +03:00
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
} else {
key := string(args[1])
mu.Lock()
val, ok := keys[key]
mu.Unlock()
2017-10-28 03:01:03 +03:00
if !ok {
out = redcon.AppendNull(out)
} else {
out = redcon.AppendBulkString(out, val)
}
2017-07-04 06:39:18 +03:00
}
2017-10-28 03:01:03 +03:00
case "SET":
if len(args) != 3 {
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
2017-07-04 06:39:18 +03:00
} else {
key, val := string(args[1]), string(args[2])
mu.Lock()
keys[key] = val
mu.Unlock()
2017-10-28 03:01:03 +03:00
out = redcon.AppendString(out, "OK")
2017-07-04 06:39:18 +03:00
}
case "DEL":
if len(args) < 2 {
2017-10-28 03:01:03 +03:00
out = redcon.AppendError(out, "ERR wrong number of arguments for '"+string(args[0])+"' command")
} else {
var n int
mu.Lock()
2017-10-28 03:01:03 +03:00
for i := 1; i < len(args); i++ {
if _, ok := keys[string(args[i])]; ok {
2017-10-28 03:01:03 +03:00
n++
delete(keys, string(args[i]))
2017-10-28 03:01:03 +03:00
}
2017-07-04 06:39:18 +03:00
}
mu.Unlock()
2017-10-28 03:01:03 +03:00
out = redcon.AppendInt(out, int64(n))
2017-07-04 06:39:18 +03:00
}
2017-10-28 22:23:13 +03:00
case "FLUSHDB":
mu.Lock()
2017-10-28 22:23:13 +03:00
keys = make(map[string]string)
mu.Unlock()
2017-10-28 22:23:13 +03:00
out = redcon.AppendString(out, "OK")
2017-07-04 06:39:18 +03:00
}
}
2017-10-28 03:01:03 +03:00
}
2017-10-28 22:23:13 +03:00
c.is.End(data)
2017-10-28 03:01:03 +03:00
return
}
var ssuf string
if stdlib {
ssuf = "-net"
}
addrs := []string{fmt.Sprintf("tcp"+ssuf+"://:%d", port)}
2017-11-02 23:29:13 +03:00
if unixsocket != "" {
addrs = append(addrs, fmt.Sprintf("unix"+ssuf+"://%s", unixsocket))
2017-11-02 23:29:13 +03:00
}
err := evio.Serve(events, addrs...)
2017-10-28 03:01:03 +03:00
if err != nil {
log.Fatal(err)
}
2017-07-04 06:39:18 +03:00
}