redis protocol server for go.

redigosrv

what

redis server侧的golang版本实现。

why

作为后端RD,你开发的,维护的最多的应该就是你的server,作为服务的提供方来说,想要和客户端进行有效的交互,首先要要能理解信息的含义,因此一套通信协议是必须的。

为什么选择redis协议,应用层有很多成熟的协议,比如

  • http
  • http2
  • websockets

当然,上述协议都很强大,但是作为内网的服务来说,需要的协议最重要的一点是可阅读的文本,因为文本协议是好理解,便于观察的,而且内网做好权限收敛即可,不需要做加密的二进制协议。

那么为什么不使用http呢?http很强大,适用范围也比redis更广。但是任何东西都是有限制的,比如:

  • 服务是存储介质/proxy
  • 协议数据包尽可能的小,提高带宽利用率

这时候redis协议更加适合!

很多开发者自己实现redis server的时候,都是自己解析协议,根据参数找到对应的handler。开发者初期通常认为程序很小,没有做抽象,因此最终的实现都耦合在一起,并且一旦规模大起来后很难再维护。

因此你需要一个库帮你屏蔽底层,提高可维护性,它拥有的能力包括:

  • 路由注册/寻址
  • 连接限制策略
  • 支持监控接口
  • 中间件能力
  • 异常捕获
  • 优雅关闭

什么,你担心不安全,放心,这是在我在百度项目中已经使用很久的库了,重构的服务支持了**500+**微服务,历经一年没出过性能问题和资源问题。并且解决了当时耦合导致的很多性能问题和资源泄露问题。

放心大胆的使用它,如果有什么需求可以直接issue搞起,长期维护。

how

go语言的net/http的基础上修改开发,接口完全兼容,将你的学习成本降到最低。你完全可以在5分钟内学会如何使用!

example中附带了一个小例子,为了方便阅读,内容直接copy到下面了。

package main

import (
	"context"
	"fmt"
	"log"
	"runtime/debug"
	"strings"
	"sync"
	"time"

	"github.com/Saner-Lee/redigosrv/redis"
)

func main() {
	ExampleServer()
}

func ExampleServer() {
	// 限制连接数量为1024
	// 达到上限时,不完全阻塞
	// 阻塞到500ms未等到可用连接返回redis错误
	sema, err := redis.NewSemaphore(1024, redis.WithBlock(false), redis.WithWaitDura(500*time.Millisecond))
	if err != nil {
		panic(err)
	}

	// cmd get -> handler
	redis.HandleFunc("GET", func(_ context.Context, w redis.ResponseWriter, r *redis.Request) {
		fmt.Printf("rece get cmd, req %v\n", r)
		w.Text("OK")
	})

	opts := []redis.Option{
		// 连接限制策略
		redis.WithSemaphore(sema),
		// 异常捕获机制
		redis.WithPanicStack(func(format string, args ...interface{}) {
			fmt.Printf("redis rece a panic, err %s", fmt.Sprintf(format, args...))
			debug.PrintStack()
		}),
		// access日志,为监控提供数据源
		redis.WithLoggerFactory(func() redis.Logger { return &logger{} }),
	}

	// 启动Server
	redis.ListenAndServe("0.0.0.0:6379", nil, opts...)
}

type logger struct {
	mu sync.RWMutex
	m  map[string]string
}

func (l *logger) prefix() string {
	l.mu.RLock()
	defer l.mu.RUnlock()

	if len(l.m) == 0 {
		return ""
	}

	var secs []string
	for k, v := range l.m {
		secs = append(secs, fmt.Sprintf("%s:%s", k, v))
	}
	return strings.Join(secs, " ")
}

func (l *logger) Debug(s string, args ...interface{}) {
	p := l.prefix()
	log.Printf(p+s, args...)
}

func (l *logger) Info(s string, args ...interface{}) {
	p := l.prefix()
	log.Printf(p+s, args...)
}

func (l *logger) Notice(s string, args ...interface{}) {
	p := l.prefix()
	log.Printf(p+s, args...)
}

func (l *logger) Warn(s string, args ...interface{}) {
	p := l.prefix()
	log.Printf(p+s, args...)
}

func (l *logger) Error(s string, args ...interface{}) {
	p := l.prefix()
	log.Printf(p+s, args...)
}

func (l *logger) AddMeta(k, v string) {
	l.mu.Lock()
	defer l.mu.Unlock()

	if l.m != nil {
		l.m = make(map[string]string)
	}
	l.m[k] = v
}
Similar Resources

godis - an old Redis client for Go

godis Implements a few database clients for Redis. There is a stable client and an experimental client, redis and exp, respectively. To use any of the

Apr 16, 2022

Google Go Client and Connectors for Redis

Go-Redis Go Clients and Connectors for Redis. The initial release provides the interface and implementation supporting the (~) full set of current Red

Oct 25, 2022

Redis client library for Go

go-redis go-redis is a Redis client library for the Go programming language. It's built on the skeleton of gomemcache. It is safe to use by multiple g

Nov 8, 2022

Type-safe Redis client for Golang

Redis client for Golang ❤️ Uptrace.dev - distributed traces, logs, and errors in one place Join Discord to ask questions. Documentation Reference Exam

Jan 4, 2023

Redis Sorted Sets Benchmark

redis-zbench-go Redis Sorted Sets Benchmark Overview This repo contains code to trigger load ( ZADD ) or query (ZRANGEBYLEX key min max) benchmarks, w

May 18, 2021

Use Redis' MONITOR to draw things in a terminal

Use Redis' MONITOR to draw things in a terminal

Redis Top Redistop uses MONITOR to watch Redis commands and shows per command and per host statistics. Because MONITOR streams back all commands, its

Aug 30, 2022

Examples and code to assign a name to your MongoDB, MySQL, PostgreSQL, RabbitMQ, and redis connection.

Examples and code to assign a name to your MongoDB, MySQL, PostgreSQL, RabbitMQ, and redis connection.

your connection deserves a name 👀 When your app interacts with an external system, assign a name to the connection. An external system in this contex

Dec 14, 2022

Schema based, typed Redis caching/memoize framework for Go

cacheme - Redis Caching Framework For Go English | 中文 Statically Typed - 100% statically typed using code generation. Scale Efficiently - thundering h

Sep 27, 2022

Redis powered simple OTS service - contains two endpoints, to create and find a secret

Onetimesecret This is a simple service that stores and finds your secret. Small but powerfull service - does not have any unnesseccery dependencies. H

Aug 20, 2022
Golang client for redislabs' ReJSON module with support for multilple redis clients (redigo, go-redis)

Go-ReJSON - a golang client for ReJSON (a JSON data type for Redis) Go-ReJSON is a Go client for ReJSON Redis Module. ReJSON is a Redis module that im

Dec 25, 2022
Redis client Mock Provide mock test for redis query

Redis client Mock Provide mock test for redis query, Compatible with github.com/go-redis/redis/v8 Install Confirm that you are using redis.Client the

Dec 27, 2022
Bxd redis benchmark - Redis benchmark tool for golang

使用 redis benchmark 工具, 测试 10 20 50 100 200 1k 5k 字节 value 大小,redis get set 性能。 r

Jan 22, 2022
High-performance framework for building redis-protocol compatible TCP servers/services

Redeo The high-performance Swiss Army Knife for building redis-protocol compatible servers/services. Parts This repository is organised into multiple

Jan 4, 2023
A Golang implemented Redis Server and Cluster.
A Golang implemented Redis Server and Cluster.

Godis is a golang implementation of Redis Server, which intents to provide an example of writing a high concurrent middleware using golang.

Dec 28, 2022
redis client implement by golang, inspired by jedis.

godis redis client implement by golang, refers to jedis. this library implements most of redis command, include normal redis command, cluster command,

Dec 6, 2022
Go client for Redis

Redigo Redigo is a Go client for the Redis database. Features A Print-like API with support for all Redis commands. Pipelining, including pipelined tr

Jan 1, 2023
Type-safe Redis client for Golang

Redis client for Golang ❤️ Uptrace.dev - distributed traces, logs, and errors in one place Join Discord to ask questions. Documentation Reference Exam

Jan 1, 2023
Go Redis Client

xredis Built on top of github.com/garyburd/redigo with the idea to simplify creating a Redis client, provide type safe calls and encapsulate the low l

Sep 26, 2022
Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more)

gokv Simple key-value store abstraction and implementations for Go Contents Features Simple interface Implementations Value types Marshal formats Road

Dec 24, 2022