By Go Language database / sql package discuss database-driven design and connection pool

1. This is the 40th article of the public No.

2. Go without knowing the language, reading this article will not be obstacle

3. Cover images from Mael BALLAND on Unsplash

  • What is the pool of technology

  • database/sql 包

    • Design Philosophy

    • Minimalist Interface

    • Call relationship

  • Connection Pool Design

    • sql.DB key attributes of the object

    • establish connection

    • Connection release

    • Clean-up connection

  • to sum up

What is the pool of technology

Pooling technology ( Pool) is a very common programming techniques, when requesting a large amount significantly optimize application performance and reduce system resource overhead frequently even built. Our daily work in common with database connection pooling, thread pooling, object pooling, etc., which are the characteristics of the "expensive" and "time-consuming" maintenance of resources in a particular "pool" in the provisions of its minimum number of connections the maximum number of connections, blocking queue configuration, etc., to facilitate unified management and reuse, also typically comes with a number of exploration alive mechanism, mandatory recycling, monitoring a kind of supporting functions.

database/sql 包

Design Philosophy

In the  Go language database operations required by the standard library  database/sql package, its upper application provides a standard  API user interface, on the exposed lower driver driving a simple interface, and internal connection pool management realized. This means that drivers of different databases can easily drive to achieve these interfaces, but no longer need to care about the details of the connection pool, just based on a single connection.

Minimalist Interface

It exposed the external interface is simple and easy to understand, which will help third parties  Driver to achieve, functional interface, including  Driver registration, Conn, Stmt, Tx, Rowsthe result set, and we by  Conn and  Stmt these two interfaces to experience the exquisite interface design (which corresponds to two interfaces  Java is  Connection and  Statement the interface, but  Go simpler)

I believe that even if you have not learned  Go the language, alone your  Java knowledge, you can effortlessly understand the meaning of these interfaces above, these layers are exposed to the drive interface is very simple, so that the driver can easily to achieve.

Call relationship

The entire  database/sql call relationship-driven interface is very clear and simple terms by drivers first  Open method to get a new  Conn, and then call  Conn the  Prepare method, passing in  SQL the statement is the statement  Stmt, the last call  Stmt the  Exec method returns a result parameter passing, query empathy , but returns the result set of data rows.

Connection Pool Design

sql.DB key attributes of the object

Go When the operation of the database language, we use the first  sql.Open method to return a specific  sql.DB target, the film following code  db :

sql.DB The object that is the entrance of our access to the database, we look at key attributes inside it, were associated with the connection pool design

establish connection

In fact, the connection is not in  sql.Open the return  db on the establishment of an object, which just opened a reception step up a connection request  channel, even the actual construction of concrete steps to be performed until  SQL when the statement will be. Here is how we achieve by telling about some examples of logical connection pool connection is established how.

This section describes the principle source does not stick too much, it becomes a source resolved, the students do not understand the language Go unfriendly, mainly hoping to convey some ideas connection pool design.

In the  database/sql upper applications exposed in the user interface, it is more commonly used  Exec and  Query, the former is used to perform the write  SQL, which can be used for reading  SQL. But regardless of which method to go, you will be called to build even logical  db.conn method, and even comes with built context and strategies to build even two parameters.

Which even built into policies  cachedOrNewConn and  alwaysNewConn. From the former preferentially  freeConn removed connection idle connection, or create a new one; the latter always walk logic new connection.

Using  cachedOrNewConn up a connection in a logical strategy, first determines whether there is an idle connection, if there is an idle connection is first removed, followed by determining whether the connection needs to be recovered expired, and if not expired can be used normally goes to a subsequent logic. If no free connection is determined that the number of connections is not already reached the maximum, if no new connection, otherwise it must wait for an available blocking connection request.

If desired new connection, is called the bottom  Driver connector implemented  Connect interface, which is part of a respective database  Driver itself to achieve.

Connection release

After the connection is a need to return to the pool after use, which is the database connection pool to achieve the more important logical, usually accompanied on the reliability of the detection connection, if the connection is closed, then returned to the connection pool should not continue, and Create a new connection should be replaced.

In  Java the  Druid connection pool will  testOnReturn or  testOnBorrow an option, it is checked for validity at the time of return connection or get a connection, but the open nature will be extended on both connection occupied time, lose some performance. Go Language to achieve this function is relatively simple, and there is no specific validity detection mechanism, but a direct connection according to the supplied  err information, if it is  ErrBadConn abnormal shut down and send a new signal.

Clean-up connection

database/sql It provides three key parameters related to the connection pool under the package, respectively  maxIdle, maxOpen and  maxLifeTime.

Meaning three parameters is easy to understand, if you want to understand, recommended reading  Configuring sql.DB for Better Performance

MySQL Side will be forced  kill out of idle connection time (8h), Go language provides  maxLifeTime the maximum time set the connection options are multiplexed, attention is not idle connection time, but to establish a connection from this point in time will be recovered, so as to ensure connection active.

This cleaning mechanism is through an asynchronous task to do, each key is logically one second traverse checking  freeConn an idle connection is determined whether multiplexing period exceeds the maximum, beyond the connecting join  Closing array is subsequently  Close.

to sum up

Recent work content is based on the  go-sql-driver realization of a custom support a separate read and write and highly available  driver, during the research and learning to feel the  Go language  database/sql clear and concise package, although it is in the realization of some of the features of simple partial or no, but still covered the main functions and features most of the database connection pool, so I think to use it to start a discussion is a good choice.

发布了77 篇原创文章 · 获赞 50 · 访问量 5万+

Guess you like

Origin blog.csdn.net/chen_kkw/article/details/99147652