2 Star 1 Fork 2

trackertrader / rpcx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oneclient_pool.go 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
wulengbing@163.com 提交于 2022-06-07 11:30 . modify
package client
import (
"sync/atomic"
"gitee.com/trackertrader/rpcx/protocol"
)
// OneClientPool is a oneclient pool with fixed size.
// It uses roundrobin algorithm to call its xclients.
// All oneclients share the same configurations such as ServiceDiscovery and serverMessageChan.
type OneClientPool struct {
count uint64
index uint64
oneclients []*OneClient
auth string
failMode FailMode
selectMode SelectMode
discovery ServiceDiscovery
option Option
serverMessageChan chan<- *protocol.Message
}
// NewOneClientPool creates a fixed size OneClient pool.
func NewOneClientPool(count int, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option) *OneClientPool {
pool := &OneClientPool{
count: uint64(count),
oneclients: make([]*OneClient, count),
failMode: failMode,
selectMode: selectMode,
discovery: discovery,
option: option,
}
for i := 0; i < count; i++ {
oneclient := NewOneClient(failMode, selectMode, discovery, option)
pool.oneclients[i] = oneclient
}
return pool
}
// NewBidirectionalOneClientPool creates a BidirectionalOneClient pool with fixed size.
func NewBidirectionalOneClientPool(count int, failMode FailMode, selectMode SelectMode, discovery ServiceDiscovery, option Option, serverMessageChan chan<- *protocol.Message) *OneClientPool {
pool := &OneClientPool{
count: uint64(count),
oneclients: make([]*OneClient, count),
failMode: failMode,
selectMode: selectMode,
discovery: discovery,
option: option,
serverMessageChan: serverMessageChan,
}
for i := 0; i < count; i++ {
oneclient := NewBidirectionalOneClient(failMode, selectMode, discovery, option, serverMessageChan)
pool.oneclients[i] = oneclient
}
return pool
}
// Auth sets s token for Authentication.
func (c *OneClientPool) Auth(auth string) {
c.auth = auth
for _, v := range c.oneclients {
v.Auth(auth)
}
}
// Get returns a OneClient.
// It does not remove this OneClient from its cache so you don't need to put it back.
// Don't close this OneClient because maybe other goroutines are using this OneClient.
func (p *OneClientPool) Get() *OneClient {
i := atomic.AddUint64(&p.index, 1)
picked := int(i % p.count)
return p.oneclients[picked]
}
// Close this pool.
// Please make sure it won't be used any more.
func (p OneClientPool) Close() {
for _, c := range p.oneclients {
c.Close()
}
p.oneclients = nil
}
Go
1
https://gitee.com/trackertrader/rpcx.git
git@gitee.com:trackertrader/rpcx.git
trackertrader
rpcx
rpcx
v1.2.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891