zoukankan      html  css  js  c++  java
  • Redcon:快速的Redis服务器Go实现

    Fast Redis compatible server framework for Go

    Redcon is a custom Redis server framework for Go that is fast and simple to use. The reason for this library it to give an efficient server front-end for the BuntDB and Tile38 projects.

    Features

    • Create a Fast custom Redis compatible server in Go
    • Simple interface. One function ListenAndServe and two types Conn & Command
    • Support for pipelining and telnet commands
    • Works with Redis clients such as redigoredis-pynode_redis, and jedis
    • TLS Support
    • Multithreaded

    Installing

    go get -u github.com/tidwall/redcon
    

    Example

    Here's a full example of a Redis clone that accepts:

    • SET key value
    • GET key
    • DEL key
    • PING
    • QUIT

    You can run this example from a terminal:

    go run example/clone.go
    package main
    
    import (
    	"log"
    	"strings"
    	"sync"
    
    	"github.com/tidwall/redcon"
    )
    
    var addr = ":6380"
    
    func main() {
    	var mu sync.RWMutex
    	var items = make(map[string][]byte)
    	go log.Printf("started server at %s", addr)
    	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]) + "'")
    			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)
    				}
    			}
    		},
    		func(conn redcon.Conn) bool {
    			// use this function to accept or deny the connection.
    			// log.Printf("accept: %s", conn.RemoteAddr())
    			return true
    		},
    		func(conn redcon.Conn, err error) {
    			// this is called when the connection has been closed
    			// log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
    		},
    	)
    	if err != nil {
    		log.Fatal(err)
    	}
    }

    TLS Example

    Redcon has full TLS support through the ListenAndServeTLS function.

    The same example is also provided for serving Redcon over TLS.

    go run example/tls/clone.go

    Benchmarks

    Redis: Single-threaded, no disk persistence.

    $ redis-server --port 6379 --appendonly no
    
    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.

    $ GOMAXPROCS=1 go run example/clone.go
    
    redis-benchmark -p 6380 -t set,get -n 10000000 -q -P 512 -c 512
    SET: 2018570.88 requests per second
    GET: 2403846.25 requests per second
    

    Redcon: Multi-threaded, no disk persistence.

    $ GOMAXPROCS=0 go run example/clone.go
    
    $ redis-benchmark -p 6380 -t set,get -n 10000000 -q -P 512 -c 512
    SET: 1944390.38 requests per second
    GET: 3993610.25 requests per second
    

    Running on a MacBook Pro 15" 2.8 GHz Intel Core i7 using Go 1.7

  • 相关阅读:
    云区域(region),可用区(AZ),跨区域数据复制(Cross-region replication)与灾备(Disaster Recovery)(部分1)
    云区域(region),可用区(AZ),跨区域数据复制(Cross-region replication)与灾备(Disaster Recovery)(部分2)
    VLAN 模式下的 OpenStack 管理 vSphere 集群方案
    理解Kubernetes(2): 应用的各种访问方式
    理解Kubernetes(1):手工搭建Kubernetes测试环境
    2017这一年(内空)
    eKing Cloud基础云平台演进之路
    《重新定义公司
    使用 Kafka 和 ELK 搭建测试日志系统(1)
    [译] OpenStack Pike 版本中的 53 个新功能盘点
  • 原文地址:https://www.cnblogs.com/ExMan/p/11835473.html
Copyright © 2011-2022 走看看