This is a creation in Article, where the information may have evolved or changed.
Golang's channel has many other functions besides goroutine communication, this paper will implement a universal connection pool based on channel.
What is GM?
The implementation of the connection pool does not depend on the specific instance, but relies on an interface, the connection pool of this article chooses the io.Closer
interface, as long as the object that implements the interface can be managed by the pool.
Of course, you can implement interface{}
a connection pool based on which any object can be managed.
Implementation principle
The connection handle is stored in the channel, and due to the characteristics of the cache channel, if there is a connection in the pool when the connection is obtained, it will be returned directly, if there is no connection in the pool, it will block or create a new connection (without exceeding the maximum limit).
Because of interface-oriented programming, all the logic to create a connection is unclear, and it is necessary to pass in a function that returns an io.Closer
object.
Realize
Because of concurrency problems, you need to lock the mutex data in the operation pool.
Package Poolimport ("Errors" "io" "Sync" "Time") var (errinvalidconfig = errors. New ("Invalid pool config") errpoolclosed = errors. New ("Pool closed")) Type Factory func () (IO. Closer, error) type Pool interface {acquire () (IO. Closer, error)//Get resource Release (IO. Closer) Error//release resource Close (IO. Closer) error//close resource Shutdown () error//close pool}type genericpool struct {sync. Mutex Pool chan io. Closer Maxopen int//Pool The maximum number of resources Numopen INT//number of resources in the current pool Minopen int//Pool minimum resource number closed BOOL// Whether the pool is closed maxlifetime time. Duration Factory Factory//Create a connection method}func Newgenericpool (minopen, Maxopen int, maxlifetime time. Duration, Factory Factory) (*genericpool, error) {if Maxopen <= 0 | | minopen > Maxopen {return nil, Erri Nvalidconfig} P: = &genericpool{maxopen:maxopen, Minopen:minopen, Maxlifetime: Maxlifetime, Factory:factory, POOl:make (Chan io. Closer, Maxopen),} for I: = 0; i < Minopen; i++ {closer, err: = Factory () If err! = nil {continue} p.numopen++ P.pool <-closer} return p, Nil}func (P *genericpool) acquire () (IO. Closer, error) {if p.closed {return nil, errpoolclosed} for {Closer, err: = P.getorcreate () If err! = Nil {return nil, err}//TODO Maxlifttime process return closer, Nil}}func ( P *genericpool) getorcreate () (IO. Closer, error) {Select {case Closer: = <-p.pool:return Closer, nil Default:} p.lock () if p . Numopen >= P.maxopen {closer: = <-p.pool P.unlock () return closer, nil}//New connection CLO Ser, err: = P.factory () if err! = Nil {p.unlock () return nil, err} p.numopen++ P.unlock () r Eturn Closer, nil}//releases a single resource to Connection pool func (P *genericpool) release (Closer IO. Closer) Error {if p.closed {return errpoolclosed} p.lock () P.pool <-closer P.unlock () return nil}//close order Resource func (P *genericpool) Close (Closer IO. Closer) Error {P.lock () Closer. Close () p.numopen--P.unlock () return nil}//Close the connection pool, freeing all resources func (P *genericpool) Shutdown () error {if p.closed {return errpoolclosed} p.lock () Close (P.pool) for closer: = Range P.pool {closer. Close () p.numopen--} p.closed = True P.unlock () return nil}
Conclusion
Based on this connection pool, you can manage all io.Closer
objects. For example memcached
, redis
wait, very convenient!