1

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:

  1. Reduce the burden on the business
  2. Improve the performance of the framework itself
  3. 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:

  1. NewResourceManager : Create a pool
  2. GetResource(key, createFunc) : The key is used to prevent repeated requests when acquiring resources concurrently, and createFunc 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 :

  1. GetResource , carrying the specific key 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 in map
  2. The query and writing of map
  3. 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 clear SharedCalls , you can check it carefully in the official go-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

kevinwan
931 声望3.5k 粉丝

go-zero作者