Ai
1 Star 3 Fork 2

tym_hmm/kafka-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Container.go 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
天蝎儿 提交于 2023-01-10 17:19 +08:00 . 完成生产者处理
package kafka_go
import (
"errors"
sarama "gitee.com/tym_hmm/go-kafa-shopify-sarama"
"gitee.com/tym_hmm/kafka-go/Stragey"
"sync"
"sync/atomic"
)
var strategyFactory = Stragey.NewStrategyFactory()
/**
生产者客户端容器
*/
type containerProduct struct {
build BuildProductApi
client []sarama.SyncProducer
clientCurrentIndex int32
rLock sync.RWMutex
}
func newContainerProduct(build BuildProductApi) *containerProduct {
return &containerProduct{
build: build,
clientCurrentIndex: 0,
}
}
/**
获取连接列表
*/
func (this *containerProduct) getClientList() []sarama.SyncProducer {
return this.client
}
/**
获取连接
*/
func (this *containerProduct) GetClient() (int32, sarama.SyncProducer) {
this.rLock.Lock()
defer this.rLock.Unlock()
currentIndex := this.clientCurrentIndex
clientLen := int32(len(this.client))
var strategyIndex int32
connStrategy := this.build.GetConnStrategy()
switch connStrategy {
case PRODUCT_CONN_STRATEGY_BALANCE_ROUNDROBIN:
strategyIndex = strategyFactory.RoundRobin(currentIndex, clientLen)
default:
strategyIndex = strategyFactory.RoundRobin(currentIndex, clientLen)
}
loadIndex := strategyIndex - currentIndex
atomic.AddInt32(&this.clientCurrentIndex, loadIndex)
return this.clientCurrentIndex, this.client[this.clientCurrentIndex]
}
/**
添加连接
*/
func (this *containerProduct) addClient(client sarama.SyncProducer) error {
if client == nil {
return errors.New("product can not be empty")
}
this.rLock.Lock()
defer this.rLock.Unlock()
if this.client == nil {
this.client = []sarama.SyncProducer{}
}
this.client = append(this.client, client)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tym_hmm/kafka-go.git
git@gitee.com:tym_hmm/kafka-go.git
tym_hmm
kafka-go
kafka-go
master

搜索帮助