Redigo 源码学习:连接池的设计

主要数据结构

暴露给外部用户的 Pool 对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
type Pool struct {
...

// Maximum number of idle connections in the pool.
MaxIdle int

// Maximum number of connections allocated by the pool at a given time.
// When zero, there is no limit on the number of connections in the pool.
MaxActive int

// Close connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.
IdleTimeout time.Duration

mu sync.Mutex // mu protects the following fields
active int // the number of open connections in the pool
idle idleList // idle connections
}

内部维护空闲/待复用的列表数据结构 idleList

Read More

Redigo 源码学习

Redigo 是目前比较流行的一个 Redis Client。

虽然我个人不太喜欢它的 API 设计,奈何这个库简单易用,连公司的 redis 基础库都是在 Redigo 上做的的一个魔改。

注:原生的 Redigo 并不支持 Redis Cluster,某B站的做法是通过 side-car 的方式启动一个 redis proxy 伴生容器,屏蔽 Redis Cluster 的通信细节。

Read More

再谈 passkey idiom

我在 Passkey Idiom 这篇文章中介绍了如何使用 passkey idiom 以及使用需要的注意项。

其中最强调的就是 Token 类的 ctor 必须同时满足 1) private access level 2) 存在显式定义;否则用户可以使用

Read More

The std::byte For Byte Addressing

时隔多年,C++ 17 终于迎来了专门用于 byte-oriented access 的数据类型 std::byte

之所以引入一个新的类型是因为之前不管是 char 还是 unsigned char,都承载了三种角色

Read More

Passkey Idiom

如果出于某些目的希望将某个类的构造函数设置为 private,并提供工厂函数 Make() 创建最终对象;工厂函数中通常会使用 std::make_unique() 或者 std::make_shared() 来创建由对应智能指针托管的对象。

Read More

Golang's Options Pattern

可以将 Golang 的 Options Pattern 视作(有副作用)函数式版的 _Builder Pattern_,其核心是:特定的 option-function 调用会生成对应的类型为 Option 的闭包,执行闭包会修改内部的 Options 结构。

Golang 的函数支持同一类型的不定参数,因此上面的闭包类型一致。

Read More

保持 ASIO io_context 无任务时运行

ASIO 的 io_context::run() 如果发现没有 pending 的任务就会返回,对于 server 的监听线程来说这是符合常理的,因为无论如何至少有个 acceptor::async_accept() 在 pending。

Read More