Preface
Some students in the group always ask go-zero
the database of 060b4351e3bf67 and the
redis
database have connection pool support. Let me talk about the conclusion first: , you can use it with confidence!
From the perspective of framework design, the resource of database connection is of course to reduce frequent operations as much as possible:
- Reduce the burden on the business
- Improve the performance of the framework itself
- Pooling technology is a generalized technology, which itself should serve as the upper-level business of a general library support framework
So whether it is sqlx
, redis
, and mongo
, the underlying pooling processing is common for the data source types that may be supported in the future; so when developers need a pooling processing component, go-zero
is also provided.
The library supported by the pooling technology is located at: core/resourcemanager.go
. Let's take a look at the use of this library~~
use
If you use it, let's look directly at sqlx
, how it is used:
// 1. 初始化
var connManager = syncx.NewResourceManager()
func getCachedSqlConn(driverName, server string) (*db, error) {
val, err := connManager.GetResource(server, func() (io.Closer, error) {
// 2. 此处才是真正创建连接的地方
conn, err := newDBConnection(driverName, server)
...
// 3. 将连接返回给连接池【内部也肯定是存起来】
return &db{
DB: conn,
}, nil
})
...
return val.(*db), nil
}
Talk about the main points:
NewResourceManager
: Create a poolGetResource(key, createFunc)
: The key is used to prevent repeated requests when acquiring resources concurrently, andcreateFunc
is the function that is being used to create resources [This function needs to be written by the developer to meet the business requirements]
Summarize the resource pool model:
// 1. new
var manager = NewResourceManager()
// 2. 业务资源获取函数
func getResource(key string) (*resource, error) {
return manager.GetResource(key, createFunc);
}
// 3.业务资源创建函数【由开发者自己编写,此处只是一个样例】
func createFunc() (io.Closer, error) {
// 打开一个资源
conn, err := openResource();
// 设置一下资源配置
conn.setConfig()
return conn, err;
}
Overall analysis
In fact, the process is very simple, and there is also our sharedCalls
:
GetResource
, carrying the specifickey
map
in the resource pool:- Find it, go back directly
- Not found, call the incoming
create()
; only here is a real resource connection created and put inmap
- The query and writing of
map
- When operating on the resource pool, add
sharedCalls
: to prevent invalid traffic requests and sharing request results during concurrent requests.
For students who are not clearSharedCalls
, you can check it carefully in the officialgo-zero
to sum up
This article go-zero
the resource pool library of 060b4351e3c245 from use to structure. resourcemanager
the logic of resource applications that often occur in your business, which essentially adds a cache to the resource to save repeated creation.
go-zero
more design and implementation articles about 060b4351e3c258, you can follow the "Microservice Practice" public account.
project address
https://github.com/tal-tech/go-zero
Welcome to use go-zero and star support us!
WeChat Exchange Group
Follow the " practice " public exchange group get the community group QR code.
For the go-zero series of articles, please refer to the "Microservice Practice" public account
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。