Aws连接池

// AWSClientPool 连接池结构体
type AWSClientPool struct {
	sync.Mutex
	S3pool  chan *s3.S3
	SESpool chan *ses.SES
	//S3pool  []*s3.S3
	//SESpool []*ses.SES
	maxSize int
}

// NewAWSClientPool 创建新的连接池
func NewAWSClientPool(poolSize int) *AWSClientPool {
	S3channels := make(chan *s3.S3, poolSize)
	SESchannels := make(chan *ses.SES, poolSize)
	pool := &AWSClientPool{
		maxSize: poolSize,
	}
	for i := 0; i < poolSize; i++ {
		sess := session.Must(session.NewSession(&aws.Config{
			Credentials: credentials.NewStaticCredentials(SESaccessKey, SESsecretKey, ""),
			//Endpoint:         aws.String(endpoint),
			Region: aws.String(SESregion),
			//S3ForcePathStyle: aws.Bool(false),
			//SDK 支持使用客户端 TLS 证书配置的环境和会话选项,这些证书作为客户端 TLS 握手的一部分发送以进行客户端身份验证。
			//如果使用,则需要 Cert 和 Key 值。如果缺少一个,或者无法加载文件的内容,则会返回一个错误。
			//ClientTLSCert:              nil,
			//ClientTLSKey:               nil,
		}))
		SESchannels <- ses.New(sess)
		S3channels <- s3.New(sess)
		//pool.SESpool = append(pool.SESpool, ses.New(sess))
		//pool.S3pool = append(pool.S3pool, s3.New(sess))
	}
	return pool
}

// GetS3Connection 从连接池获取一个 S3 客户端
func (p *AWSClientPool) GetS3Connection() *s3.S3 {
	p.Lock()
	defer p.Unlock()
	return <-p.S3pool

}

// GetSESConnection 从连接池获取一个 SES 客户端
func (p *AWSClientPool) GetSESConnection() *ses.SES {
	p.Lock()
	defer p.Unlock()

	//if len(p.SESpool) > 0 {
	//	conn := p.SESpool[0]
	//	p.SESpool = p.SESpool[1:] // 移除并返回第一个连接
	//	return conn
	//}
	return <-p.SESpool
}

// ReturnS3Connection 将连接返回到连接池
func (p *AWSClientPool) ReturnS3Connection(conn *s3.S3) {
	p.Lock()
	defer p.Unlock()
	//
	//if len(p.sessions) < p.maxSize {
	//	p.sessions = append(p.sessions, conn) // 重新加入连接池
	//}
	p.S3pool <- conn
}

// ReturnSESConnection 将连接返回到连接池
func (p *AWSClientPool) ReturnSESConnection(conn *ses.SES) {
	p.Lock()
	defer p.Unlock()
	//
	//if len(p.sessions) < p.maxSize {
	//	p.sessions = append(p.sessions, conn) // 重新加入连接池
	//}
	p.SESpool <- conn
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值