关于go访问rabbitmq的连接池

转载地址:https://www.golangtc.com/t/54ca4877421aa9537400014c

下 https://github.com/owner888/epooll/blob/master/pool.go 内容

package epooll

import (
    "time"
    //"fmt"
    //"reflect"
)

var nowFunc = time.Now // for testing

type ConnPool struct {
    // Dial is an application supplied function for creating and configuring a
    // connection
    Dial func() (interface{}, error)
    //Dial func() (*autorc.Conn, error)

    // Maximum number of idle connections in the pool.
    MaxIdle int

    // Maximum number of connections allocated by the pool at a given time.
    // When zero, there is no limit on the number of connections in the pool.
    MaxActive int

    closed bool
    active int

    idle chan interface{}
}

type idleConn struct {
    c interface{}
    t time.Time
}

// 批量生成连接,并把连接放到连接池channel里面
func (this *ConnPool)InitPool() error{
    this.idle = make(chan interface{}, this.MaxActive)
    for x := 0; x < this.MaxActive; x++ {
        //conn, err := this.Dial()
        // 这里返回DB类,而不是返回mysql.Conn,否则DB类的insert,update 这些Active Record类方法没法使用
        db, err := this.Dial()
        //fmt.Println(" --- reflect --- ", reflect.TypeOf(db))
        if err != nil {
            return err
        }
        //this.idle <-conn
        this.idle <-idleConn{t: nowFunc(), c: db}
    }
    return nil
}

// 从连接池里取出连接
func (this *ConnPool)Get() interface{} {
    // 如果空闲连接为空,初始化连接池
    if this.idle == nil {
        this.InitPool()
    }

    // 赋值一下好给下面回收和返回
    //conn := <-this.idle
    //idleConn
    ic := <-this.idle
    // 这里要用 (idleConn) 把interface{} 类型转化为 idleConn 类型的,否则拿不到里面的属性t、c
    conn := ic.(idleConn).c
    //fmt.Println(conn.(*DB).conn)
    //fmt.Println(" --- reflect --- ", reflect.TypeOf(conn))

    // 使用完把连接回收到连接池里
    defer this.Release(conn)

    // 因为channel是有锁的,所以就没必要借助sync.Mutex来进行读写锁定
    // container/list就需要锁住,不然并发就互抢出问题了
    return conn
}

// 回收连接到连接池
func (this *ConnPool)Release(conn interface{}) {
    //this.idle <-conn
    this.idle <-idleConn{t: nowFunc(), c: conn}
}

封装的rabbitpool

package epooll
import (
    "fmt"
    "github.com/astaxie/beego"
    "github.com/streadway/amqp"
)
func RabbitPool() *ConnPool {
    qUser := beego.AppConfig.String("RABBITMQ::user")
    qPass := beego.AppConfig.String("RABBITMQ::pass")
    qHost := beego.AppConfig.String("RABBITMQ::host")
    qPort, _ := beego.AppConfig.Int("RABBITMQ::port")
    rabbitmq := fmt.Sprintf("amqp://%s:%s@%s:%d/", qUser, qPass, qHost, qPort)
    poolNum := 10
    fmt.Printf("初始化 Mysql 连接池,连接数:%d \n", poolNum)
    cp := &ConnPool{
        MaxActive: poolNum,
        Dial: func() (interface{}, error) {
            conn, err := amqp.Dial(rabbitmq)
            return conn, err
        },
    }
    return cp
}

使用:

cp := epooll.RabbitPool()
fmt.Println(&cp)
conn := cp.Get().(*amqp.Connection)

这里的&cp 每次的地址都不一样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值