2016-08-20 20:23:11 +03:00
< p align = "center" >
< img
src="logo.png"
2022-04-18 12:04:21 +03:00
width="336" border="0" alt="REDCON">
2016-08-20 20:23:11 +03:00
< br >
< a href = "https://godoc.org/github.com/tidwall/redcon" > < img src = "https://img.shields.io/badge/api-reference-blue.svg?style=flat-square" alt = "GoDoc" > < / a >
< / p >
2016-07-28 17:54:02 +03:00
2021-02-12 02:30:40 +03:00
< p align = "center" > Redis compatible server framework for Go< / p >
2016-08-20 20:23:11 +03:00
Features
--------
2016-08-20 21:30:29 +03:00
- Create a [Fast ](#benchmarks ) custom Redis compatible server in Go
2016-09-18 06:01:16 +03:00
- Simple interface. One function `ListenAndServe` and two types `Conn` & `Command`
2016-08-20 21:23:34 +03:00
- Support for pipelining and telnet commands
2016-08-20 20:23:11 +03:00
- Works with Redis clients such as [redigo ](https://github.com/garyburd/redigo ), [redis-py ](https://github.com/andymccurdy/redis-py ), [node_redis ](https://github.com/NodeRedis/node_redis ), and [jedis ](https://github.com/xetorthio/jedis )
2017-09-22 21:54:30 +03:00
- [TLS Support ](#tls-example )
2020-10-31 17:31:40 +03:00
- Compatible pub/sub support
2019-05-06 19:04:21 +03:00
- Multithreaded
2016-08-20 21:23:34 +03:00
2016-07-28 20:43:10 +03:00
Installing
----------
```
2016-08-20 20:23:11 +03:00
go get -u github.com/tidwall/redcon
2016-07-28 20:43:10 +03:00
```
2017-09-22 21:54:30 +03:00
Example
2016-07-28 20:43:10 +03:00
-------
2016-09-18 06:01:16 +03:00
2016-07-28 17:54:02 +03:00
Here's a full example of a Redis clone that accepts:
- SET key value
- GET key
- DEL key
- PING
- QUIT
2020-10-31 17:51:53 +03:00
- PUBLISH channel message
- SUBSCRIBE channel
2016-07-28 17:54:02 +03:00
You can run this example from a terminal:
```sh
2016-08-20 21:34:06 +03:00
go run example/clone.go
2016-07-28 17:54:02 +03:00
```
```go
package main
import (
"log"
"strings"
"sync"
"github.com/tidwall/redcon"
)
var addr = ":6380"
func main() {
var mu sync.RWMutex
2016-09-18 06:01:16 +03:00
var items = make(map[string][]byte)
2020-10-31 17:30:48 +03:00
var ps redcon.PubSub
2016-07-28 17:54:02 +03:00
go log.Printf("started server at %s", addr)
err := redcon.ListenAndServe(addr,
2016-09-18 06:01:16 +03:00
func(conn redcon.Conn, cmd redcon.Command) {
switch strings.ToLower(string(cmd.Args[0])) {
default:
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
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
}
2020-10-31 17:51:53 +03:00
case "publish":
if len(cmd.Args) != 3 {
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
return
}
conn.WriteInt(ps.Publish(string(cmd.Args[1]), string(cmd.Args[2])))
case "subscribe", "psubscribe":
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]))
}
}
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)
}
}
```
2017-09-22 21:54:30 +03:00
TLS Example
-----------
2017-09-12 13:00:04 +03:00
2017-09-22 21:54:30 +03:00
Redcon has full TLS support through the `ListenAndServeTLS` function.
2017-09-12 13:00:04 +03:00
2017-09-22 21:54:30 +03:00
The [same example ](example/tls/clone.go ) is also provided for serving Redcon over TLS.
2017-09-12 13:00:04 +03:00
2017-09-22 21:54:30 +03:00
```sh
go run example/tls/clone.go
2017-09-12 13:00:04 +03:00
```
2016-08-20 21:23:34 +03:00
Benchmarks
----------
**Redis**: Single-threaded, no disk persistence.
2016-08-20 21:45:35 +03:00
```
$ redis-server --port 6379 --appendonly no
```
2016-08-20 21:23:34 +03:00
```
redis-benchmark -p 6379 -t set,get -n 10000000 -q -P 512 -c 512
SET: 941265.12 requests per second
GET: 1189909.50 requests per second
```
**Redcon**: Single-threaded, no disk persistence.
2016-08-20 21:45:35 +03:00
```
$ GOMAXPROCS=1 go run example/clone.go
```
2016-08-20 21:23:34 +03:00
```
redis-benchmark -p 6380 -t set,get -n 10000000 -q -P 512 -c 512
2016-09-18 06:01:16 +03:00
SET: 2018570.88 requests per second
GET: 2403846.25 requests per second
2016-08-20 21:23:34 +03:00
```
**Redcon**: Multi-threaded, no disk persistence.
```
2016-08-20 21:45:35 +03:00
$ GOMAXPROCS=0 go run example/clone.go
```
```
$ redis-benchmark -p 6380 -t set,get -n 10000000 -q -P 512 -c 512
2016-09-18 06:01:16 +03:00
SET: 1944390.38 requests per second
GET: 3993610.25 requests per second
2016-08-20 21:23:34 +03:00
```
2016-08-20 21:31:28 +03:00
*Running on a MacBook Pro 15" 2.8 GHz Intel Core i7 using Go 1.7*
2016-08-20 21:23:34 +03:00
2016-07-28 17:54:02 +03:00
Contact
-------
Josh Baker [@tidwall ](http://twitter.com/tidwall )
License
-------
Redcon source code is available under the MIT [License ](/LICENSE ).