Golang Universal Connection Pool Golang-pool

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.