This is a creation in Article, where the information may have evolved or changed.
Connection pool implemented by Golang
Function:
* Connection pool has a connection type of interface{}, making it more versatile
* Link Maximum idle time, timeout link will turn off discard, can avoid link auto-invalidation problem when idle
* Use channel processing links in the pool to efficiently
Basic usage
Factory method for creating a connection factory: = Func () (interface{}, error) {return net. Dial ("TCP", "127.0.0.1:4000")}//close method of closing the link close: = Func (v interface{}) error {return V. (NET). Conn). Close ()}//Creates a connection pool: Initialize 5, maximum link 30poolConfig: = &pool. poolconfig{Initialcap:5, maxcap:30, Factory:factory, close:close,//link maximum idle time, links over that time will be closed , can avoid link EOF when idle, automatic invalidation problem idletimeout:15 * time. Second,}p, err: = Pool. Newchannelpool (poolconfig) if err! = Nil {fmt. Println ("err=", err)}//get a link from the connection pool V, err: = P.get ()//do something//conn=v. (NET. Conn)//Put the link back in the connection pool p. Put (v)//Releases all links in the connection pool p. Release ()//view the current number in the link: = P.len ()
Https://github.com/silenceper/pool