Go语言TCP连接池 rocket049/connpool

fuhuizn · 2019-06-17 10:59:11 · 982 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2019-06-17 10:59:11 的主题,其中的信息可能已经有所发展或是发生改变。

rocket049/connpool包是本人用go语言开发的,提供一个通用的TCP连接池,初始化参数包括最高连接数、超时秒数、连接函数,放回连接池的连接被重新取出时,如果已经超时,将会自动重新连接;如果没有超时,连接将被复用。

安装( go mod 模式无需安装,可以直接引用 ):

go get -v -u github.com/rocket049/connpool

go get -v -u gitee.com/rocket049/connpool

主页: https://gitee.com/rocket049/connpool/

README.md文件有详细使用说明。

软件架构

type Conn
    func (s *Conn) Close() error
    func (s *Conn) Read(p []byte) (int, error)
    func (s *Conn) Timeout() bool
    func (s *Conn) Write(p []byte) (int, error)
type Pool
    func NewPool(max, timeout int, factory func() (net.Conn, error)) *Pool
    func (s *Pool) Close()
    func (s *Pool) Get() (*Conn, error)
    func (s *Pool) Put(conn1 *Conn)

type Conn
    Conn - Wrap of net.Conn

    type Conn struct {
        // contains filtered or unexported fields
    }

func (s *Conn) Close() error
    Close - Close the connection


func (s *Conn) Read(p []byte) (int, error)
    Read - Compatible io.Reader

func (s *Conn) Timeout() bool
    Timeout - Test the connection is timeout. return true/false.

func (s *Conn) Write(p []byte) (int, error)
    Write - Compatible io.Write

type Pool
    Pool - Please create Pool with function NewPool

    type Pool struct {
        // contains filtered or unexported fields
    }

func NewPool(max, timeout int, factory func() (net.Conn, error)) *Pool
    NewPool - Create a new Pool struct,and initialize it.

    max - the max connection number.
    timeout - the program will use it to set deadline value.
    factory - wrap of net.Dial(...).

func (s *Pool) Close()
    Close - Close all connections in this Pool

func (s *Pool) Get() (*Conn, error)
    Get - Get a new connection from the Pool

func (s *Pool) Put(conn1 *Conn)
    Put - Put a connection back to the Pool

使用说明

import "github.com/rocket049/connpool"
//import "gitee.com/rocket049/connpool"

func factory() (net.Conn,error) {
    return net.Dial("tcp","127.0.0.1:7060")
}

func UsePool() {
    pool1 := connpool.NewPool(10, 30 ,factory)
    defer pool1.Close()
    var wg sync.WaitGroup
    for i:=0;i<50;i++ {
        wg.Add(1)
        go func(n int){
            // connect
            conn ,err := pool1.Get()
            if err!=nil {
                ...
            }
            //send
            _,err = conn.Write( msg )
            if err!=nil{
                ...
            }
            //recv
            n1,err := conn.Read( buf )
            if err!=nil{
                ...
            }
            //timeout
            if conn.Timeout() {
                pool1.Put(conn)
                conn ,err := pool1.Get()
                ...
            }
            //close
            pool1.Put(conn)
            wg.Done()
        }(i)
    }
    wg.Wait()

}

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

982 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传