// 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
}
Aws连接池
最新推荐文章于 2024-09-26 10:37:29 发布