redcon/example/clone.go

121 lines
3.2 KiB
Go
Raw Normal View History

2016-07-28 17:54:02 +03:00
package main
import (
"log"
2016-09-10 07:43:07 +03:00
"strings"
2016-07-28 17:54:02 +03:00
"sync"
"github.com/tidwall/redcon"
)
var addr = ":6380"
func main() {
var mu sync.RWMutex
var items = make(map[string][]byte)
2020-10-30 16:46:06 +03:00
var ps redcon.PubSub
2016-07-28 17:54:02 +03:00
go log.Printf("started server at %s", addr)
2021-01-17 18:27:23 +03:00
2021-01-17 18:42:41 +03:00
err := redcon.ListenAndServe(addr,
func(conn redcon.Conn, cmd redcon.Command) {
switch strings.ToLower(string(cmd.Args[0])) {
default:
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
2020-10-30 16:46:06 +03:00
case "publish":
2020-10-31 17:30:48 +03:00
// Publish to all pub/sub subscribers and return the number of
// messages that were sent.
2020-10-30 16:46:06 +03:00
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
count := ps.Publish(string(cmd.Args[1]), string(cmd.Args[2]))
conn.WriteInt(count)
case "subscribe", "psubscribe":
2020-10-31 17:30:48 +03:00
// Subscribe to a pub/sub channel. The `Psubscribe` and
// `Subscribe` operations will detach the connection from the
// event handler and manage all network I/O for this connection
// in the background.
2020-10-30 16:46:06 +03:00
if len(cmd.Args) < 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
command := strings.ToLower(string(cmd.Args[0]))
for i := 1; i < len(cmd.Args); i++ {
if command == "psubscribe" {
ps.Psubscribe(conn, string(cmd.Args[i]))
} else {
ps.Subscribe(conn, string(cmd.Args[i]))
}
}
case "detach":
hconn := conn.Detach()
log.Printf("connection has been detached")
go func() {
defer hconn.Close()
hconn.WriteString("OK")
hconn.Flush()
}()
case "ping":
conn.WriteString("PONG")
case "quit":
conn.WriteString("OK")
conn.Close()
case "set":
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
mu.Lock()
items[string(cmd.Args[1])] = cmd.Args[2]
mu.Unlock()
conn.WriteString("OK")
case "get":
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
mu.RLock()
val, ok := items[string(cmd.Args[1])]
mu.RUnlock()
if !ok {
conn.WriteNull()
} else {
conn.WriteBulk(val)
}
case "del":
if len(cmd.Args) != 2 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
mu.Lock()
_, ok := items[string(cmd.Args[1])]
delete(items, string(cmd.Args[1]))
mu.Unlock()
if !ok {
conn.WriteInt(0)
} else {
conn.WriteInt(1)
2016-07-28 17:54:02 +03:00
}
2021-01-17 18:27:23 +03:00
case "config":
// This simple (blank) response is only here to allow for the
// redis-benchmark command to work with this example.
conn.WriteArray(2)
conn.WriteBulk(cmd.Args[2])
conn.WriteBulkString("")
2016-07-28 17:54:02 +03:00
}
},
func(conn redcon.Conn) bool {
2020-10-31 17:30:48 +03:00
// Use this function to accept or deny the connection.
2016-08-16 15:51:20 +03:00
// log.Printf("accept: %s", conn.RemoteAddr())
2016-07-28 17:54:02 +03:00
return true
},
func(conn redcon.Conn, err error) {
2020-10-31 17:30:48 +03:00
// This is called when the connection has been closed
2016-08-16 15:51:20 +03:00
// log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
2016-07-28 17:54:02 +03:00
},
)
if err != nil {
log.Fatal(err)
}
}